String Objects and String Literals

Hey Everyone,

I understand that strings are objects of the class string, so when you store a string in a variable, it's actually a reference to the string object.

But where is the string literal being stored?

I feel like I'm missing something essential to understanding this...

String literals if they do not used to initialize a character array are stored in static memory. For example

char s[] = "String Literal";

Here the string literal is not stored in the program. It is used only to initialize the array.

const char *s = "String Literal";

Here the string literal is stored in static memory of the program and has type const char[15].
Last edited on
Great explanation.

Thanks vlad!
all string literals are stored in static memory (although compilers are permitted to optimize them out when possible)
Last edited on
Topic archived. No new replies allowed.