1

In a simple program like the following, where is the string literal stored? Is there a chance that the memory is overwritten between the call to GetMyString() and the output?

const char* GetMyString()
{
    const char* myString = "This is my string";

    return myString;
}

int main()
{
    const char* st = GetMyString();

    // do whatever

    cout << st;
    return 0;
}
  • 1
    No there is no chance (except when your program is bugged). – john Jun 02 '23 at 17:09
  • 2
    String literals are stored in a fixed location for the entire duration of your program. A program without Undefined Behavior will never overwrite a string literal. On common systems, any attempt to overwrite this memory will crash the program. – Drew Dormann Jun 02 '23 at 17:09
  • 2
    Dupe/related: [String literals: Where do they go?](https://stackoverflow.com/questions/2589949/string-literals-where-do-they-go) – Jason Jun 02 '23 at 17:10
  • 1
    In an executable file, the string literals are either stored in the Read-Only section or they are in the executable (code / text) section somewhere near where they are accessed. When stored in the platform, they can be placed anywhere. For example, in embedded systems, they could be placed in Flash or other Read-Only Memory device. This explanation can be changed by changing the instructions to the Linker. – Thomas Matthews Jun 02 '23 at 17:20
  • Your code is incorrect, you have a local stack variable and return a pointer to that. – Pepijn Kramer Jun 02 '23 at 18:01
  • If you want to know more then use compiler explorer which will show you some of the assembly details : https://godbolt.org/. Most likely the constant strings end up in a (read only) data segment, which can be loaded into read-only page(s) of your computer – Pepijn Kramer Jun 02 '23 at 18:03
  • I remember that the old C compilers (at least the ones on Windows) would have string literals stored in writeable memory -- this may be back in the late 80's early 90's. Then you had an influx of poorly written Window/DOS `C` programs that when ported to Unix, crashed. The culprit was the modifying of string-literals. – PaulMcKenzie Jun 02 '23 at 19:40
  • @PepijnKramer I had that hunch and hence my question. I see this kind of thing a lot and looks suspicious to me. But seems to work though. Ran that code and the memory address of the literal inside the function is the same as the pointer which catches the return value. But that memory shouldn't be valid anymore. Is this correct? – aritosteles Jun 02 '23 at 20:07
  • No it is okay, the string literals are stored in a datasegment in your executable file, and this data segment will get will get loaded into memory. Simplified it is already loaded in memory (by your OS) before your programs starts and your program can use it. Then C++ code runs... exits main... and the data segment is still in memory until OS terminates the process. – Pepijn Kramer Jun 02 '23 at 20:27
  • This might interest you : [CppCon 2018: Matt Godbolt “The Bits Between the Bits: How We Get to main()”](https://www.youtube.com/watch?v=dOfucXtyEsU) – Pepijn Kramer Jun 02 '23 at 20:27

0 Answers0