49

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yesraaj
  • 46,370
  • 69
  • 194
  • 251

4 Answers4

55

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.

James Hopkin
  • 13,797
  • 1
  • 42
  • 71
45

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • The BSS segment only ever held zeroed but non-constant data; data initialized to non-zero values was in the DATA segment. – Jonathan Leffler Aug 04 '11 at 22:21
  • @Destructor, I'm assuming that was meant as a comment on the answer from JamesHopkin. *That's* the answer that contends that "string literals are valid for the entire duration of the program, even during the destruction of static objects". Neither my answer nor that of unwind (the other that you put this comment on) contends this but at least you have James surrounded :-) – paxdiablo Feb 21 '17 at 12:52
3

Well ... Yes. They sort of have to be; the information that makes up the sequence of characters in each string has to be somewhere. If they were to be allocated dynamically and then initialized, where would the information used to to the initialization reside? Thus, it's more efficient to simply make the strings static, so that they are always available and valid once the program is done loading.

unwind
  • 391,730
  • 64
  • 469
  • 606
-1

String literals are stored in read-only segments of memory

Samiksha
  • 6,122
  • 6
  • 29
  • 28
  • 6
    Likely, but not required. A conforming implementation *could* store string literals in read-write memory. Any program that can tell the difference has undefined behavior anyway. Of course storing them in read-only memory is a good idea (if the underlying system supports it). – Keith Thompson Sep 15 '11 at 04:42
  • On a (simple) microcontroller they are usually stored in RAM (without any write protection). Putting them in flash memory to save space requires special measures. – Peter Mortensen Oct 22 '22 at 04:30