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?
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 declaringT x1;,T x2;, …,T xN;. An instance ofstd::stringalways 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: