Where are string literals stored on?

We’ve already seen string literals, where a string value is hardcoded into our program.

This is from chapter 4 of “the book”.

Where are string literals stored on, stack or somewhere else?
What does the word hardcoded imply?

1 Like

String literals are constant values baked into the executable ("program image") as raw byte data. They are stored along with the other binary data that is the machine code itself executed by the CPU. If you use an appropriate tool, for example, strings, or maybe even ripgrep (rg), you can extract the string literals from the executable.

6 Likes
1 Like

@skysch, your (implied) answer is misleading at best. Exact definitions will vary based on OS and even platform, but the relevant section of your link:

The data segment is read/write, since the values of variables can be altered at run time. This is in contrast to the read-only data segment ( rodata segment or .rodata ), which contains static constants rather than variables[.]

String constants, unless somehow mutated (which would be a very rare thing in rust), are going to end up in .rodata. At least on my platform this actually ends up in the same segment as .text, not .data.

4 Likes