Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a string literal in С++ created in static memory?

Tags:

c++

string

char

Is a string literal in c++ created in static memory and destroyed only when the program exits?

like image 748
yesraaj Avatar asked Dec 08 '08 09:12

yesraaj


People also ask

Are string literals static in C?

String literals have static storage duration, and thus exist in memory for the life of the program. String literals can be used to initialize character arrays.

Where are string literals stored C memory?

String literals are stored in C as an array of chars, terminted by a null byte. A null byte is a char having a value of exactly zero, noted as '\0'. Do not confuse the null byte, '\0', with the character '0', the integer 0, the double 0.0, or the pointer NULL.

Are string literals stored in stack or heap C?

String literals are not stored in the heap or the stack, they are stored directly in your program's binary.

How are C++ strings stored in memory?

In C++, an extra byte is appended to the end of string literals when they are stored in memory. In this last byte, the number 0 is stored. It is called the null terminator or null characters, and it marks the end of the string.


2 Answers

Yes, string literals are valid for the entire duration of the program, even during the destruction of static objects.

2.13.4/1 in the Standard says

An ordinary string literal has type "array of n const char" and static storage duration.

The Standard says of 'static storage duration' in 3.7.1/1:

The storage for these objects shall last for the duration of the program.

like image 194
James Hopkin Avatar answered Sep 21 '22 10:09

James Hopkin


Where it's created is an implementation decision by the compiler writer, really. Most likely, string literals will be stored in read-only segments of memory since they never change.

In the old compiler days, you used to have static data like these literals, and global but changeable data. These were stored in the TEXT (code) segment and DATA (initialised data) segment.

Even when you have code like char *x = "hello";, the hello string itself is stored in read-only memory while the variable x is on the stack (or elsewhere in writeable memory if it's a global). x just gets set to the address of the hello string. This allows all sorts of tricky things like string folding, so that "invalid option" (0x1000) and "valid option" (0x1002) can use the same memory block as follows:

+-> plus:0   1   2   3   4   5   6   7   8   9   A   B   C   D   E |      +---+---+---+---+---+---+---+---+---+---+---+---+---+---+----+ 0x1000 | i | n | v | a | l | i | d |   | o | p | t | i | o | n | \0 |        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+----+ 

Keep in mind I don't mean read-only memory in terms of ROM, just memory that's dedicated to storing unchangeable stuff (which may be marked really read-only by the OS).

They're also never destroyed until main() exits.

like image 31
paxdiablo Avatar answered Sep 25 '22 10:09

paxdiablo