In C, I believe that the only guarantee for a string literal is that it will evaluate to a pointer to a readable region of memory, which, assuming the program is not involved in Undefined Behavior, always contains the specified characters with a null byte. The compiler and linker are allowed to work together in any mode convenient for them to make this happen. Although I donβt know of any compiler / linker systems that do this, it would be completely legal if the compiler put each string literal in its own constant section, and for the linker to place such sections in the reverse order of length and before how to place them, check if the corresponding sequence of bytes has been placed. Note that a sequence of bytes does not even have to be a string literal or a specific constant; if the linker tries to place the string "Hi!" , and he notices that the machine code contains a sequence of bytes [0x48, 0x69, 0x21, 0x00], a literal can evaluate a pointer to the first one.
Note that writing to the memory pointed to by a string literal is an Undefined Behavior. In a different system, writing can be a trap, do nothing or affect only a literal written, but can also have completely unpredictable consequences (for example, if a literal is evaluated by a pointer to some machine code].
source share