Is the code below safe? It might be tempting to write code akin to this:
#include
The Standard does not guarantee the addresses of string literals with the same content will be the same. In fact, [lex.string]/16 says:
Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified.
The second part even says you might not get the same address when a function containing a string literal is called a second time! Though I've never seen a compiler do that.
So using the same character array object when a string literal is repeated is an optional compiler optimization. With my installation of g++ and default compiler flags, I also find I get the same address for two identical string literals in the same translation unit. But as you guessed, I get different ones if the same string literal content appears in different translation units.
A related interesting point: it's also permitted for different string literals to use overlapping arrays. That is, given
const char* abcdef = "abcdef";
const char* def = "def";
const char* def0gh = "def\0gh";
it's possible you might find abcdef+3
, def
, and def0gh
are all the same pointer.
Also, this rule about reusing or overlapping string literal objects applies only to the unnamed array object directly associated with the literal, used if the literal immediately decays to a pointer or is bound to a reference to array. A literal can also be used to initialize a named array, as in
const char a1[] = "XYZ";
const char a2[] = "XYZ";
const char a3[] = "Z";
Here the array objects a1
, a2
and a3
are initialized using the literal, but are considered distinct from the actual literal storage (if such storage even exists) and follow the ordinary object rules, so the storage for those arrays will not overlap.