1

As far as I know arrays are allocated statically but strings are dynamic as their length frequently changes during runtime.

What happens when I define an array like this:

std::string array[] = {"abc", "defghi", "jk", "lmnop", "qrstuvwxyz"};?

Is there a limited amount of memory allocated for every string? Or is the array allocated dynamically?

1
  • No, there isn't any limitation unless you blow off the stack, AFAIK. array resides on stack and you are copying string literals to it. Commented Oct 5, 2011 at 20:22

3 Answers 3

3

Don't overthink things. When you say T x[N];, you declare an automatic (i.e. scope-local) array. This is very similar to just declaring T x1;, T x2;, ..., T xN;. An instance of std::string always occupies the same, small size in its declaration context (e.g. on the stack); it is only the memory which it manages (by default on the free store) that is dynamic.

Note that when you write std::string s("Hello"); in your code, then the string literal (which is passed to the constructor) is of course stored in your program binary somewhere, and it gets loaded into the program memory (usually a read-only data segment). So if you really just need to read those strings (as opposed to, say, modify them), then you might as well just declare an array of char pointers and save some memory:

const char * strings[] = { "Hello", "World", "!" };   // just one copy in r/o memory
Sign up to request clarification or add additional context in comments.

Comments

2

The Literal strings are stored in the data segment of the binary.

Edit:

Good point in the comment by ildjarn. The std::strings are created by copy constructor in the arrays. Where they are stored is implementation dependent. Some implementation store small strings (less than 32 chars) inline in an array. Others will allocate from the std::allocator which is typically malloc-ed from the heap.

1 Comment

Those questions are about string literals; this question is about std::strings.
1

In this case the compiler places those literals in the .data section of your ELF binary. So in this case they are allocated statically in the executable (or library) file that you create.

The .data section is usually reserved for compile time constants that aren't just hardcoded into the assembly instructions. As you can see it's much cheaper to simply save an address to the string than to put it everywhere it is used (as is done by some integers and defined macros).

3 Comments

The C-strings used to initialize the std::strings are in the .data section, but the std::strings themselves are not -- I believe the OP is asking about the std::strings.
@ildjarn I was assuming he meant the literals. Although I've heard that there are optimizations for std::string which allow their data to be allocated statically (not sure though)
Some std::basic_string<> implementations use what's called a "small string optimization" which allows strings below a certain size threshold to be contained directly inside of the basic_string<> object instead of on the free store (I believe this is what you're thinking of), but the basic_string<> itself must still be initialized dynamically because it is a non-POD type (only const POD types can be truly statically initialized, i.e. at compile-time).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.