In testing the following:
char* x = "Hello";
printf("%s %p %p %p", "Hello", x, "Hello", "Hello");
Hello
0x5618b7d478c7
0x5618b7d478c7
0x5618b7d478c7
I noticed that the "Hello"
has the same memory address in all cases, meaning that the compiler stores the string "Hello"
one time in .rodata
(as it should).
Is this part of the language specification in that whenever a string-literal is used it should be looked up to see if it already exists before storing it? Or is this considered a compiler optimization to not store duplicate strings?
In general the behavior depends on compiler options. You can set that the compiler will store identical string literals as one string literal or as different string literals.
So you should not relay on a condition like this
if ( "Hello" == "Hello" )
that it will be evaluated as true.
According to the C Standard (6.4.5 String literals)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.