String literals have only one instance?

Johannes Pfau spam at example.com
Thu Aug 19 06:00:26 PDT 2010


On 19.08.2010 09:53, Rory Mcguire wrote:
> Are all string literals that have the same value initialized to the same 
> address?
> 
> void main() {
> 	string same() {
> 		return "This";
> 	}
> 	assert("This" is same());
> 	assert("This" is "This");
> }
> 
> 
> Can this be relied upon?
I don't think so. It might work now, as we only have static linking, but
what happens if we have 2 independent shared libraries with the string
"This"? Each library has to include the string because the libraries
don't depend on each other, but as soon as a program uses both libraries
there are 2 memory locations where the string could be. (I guess the
linker won't do some magic to make these point at the same location. But
I might be wrong.)

-- 
Johannes Pfau


More information about the Digitalmars-d-learn mailing list