I always try to avoid to return string literals, because I fear they aren’t defined outside of the function. But I’m not sure if this is the case. Let’s take, for example, this function:
const char * return_a_string(void) { return "blah"; }
Is this correct code? It does work for me, but maybe it only works for my compiler (gcc). So the question is, do (string) literals have a scope or are they present/defined all the time.
This code is fine across all platforms. The string gets compiled into the binary as a static string literal. If you are on windows for example you can even open your .exe with notepad and search for the string itself.
Since it is a static string literal scope does not matter.
String pooling:
One thing to look out for is that in some cases, identical string literals can be ‘pooled’ to save space in the executable file. In this case each string literal that was the same could have the same memory address. You should never assume that it will or will not be the case though.
In most compilers you can set whether or not to use static string pooling for stirng literals.
Maximum size of string literals:
Several compilers have a maximum size for the string literal. For example with VC++ this is approximately 2,048 bytes.
Modifying a string literal gives undefined behavior:
Modifying a string literal should never be done. It has an undefined behavior.
Wide string literals:
All of the above applies equally to wide string literals.
Example: L’this is a wide string literal’;
The C++ standard states: (section lex.string)