Thread overview
String literals have only one instance?
Aug 19, 2010
Rory Mcguire
Aug 19, 2010
Jonathan Davis
Aug 19, 2010
Stewart Gordon
Aug 19, 2010
bearophile
Aug 19, 2010
Johannes Pfau
Aug 19, 2010
Simen kjaeraas
Aug 19, 2010
Sean Kelly
Aug 20, 2010
Rory Mcguire
August 19, 2010
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?
August 19, 2010
On 8/19/10, Rory Mcguire <rjmcguire@gm_no_ail.com> 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?
>

Well, since in Windows at least, string literals can be concatenated to and whatnot, I very much doubt that there's any sharing involved. You can always check with the is operator though. If it reports true, then the two strings have the same instance. If it reports false, then they don't.
August 19, 2010
Rory Mcguire:

> Are all string literals that have the same value initialized to the same
> address?
> ...
> Can this be relied upon?

Probably a conforming D implementation is free to not give the same address to those.

Bye,
bearophile
August 19, 2010
Jonathan Davis wrote:
<snip>
> You can always check with the is operator though. If it reports true,
> then the two strings have the same instance. If it reports false, then
> they don't.

I can't see how testing each string literal to see if it's the same instance as another can work.

The OP's point is: Are identical string literals *guaranteed* to be the same instance?  Regardless of implementation?  Regardless of whether they're next to each other, in different modules or anything in between?  Regardless of the phase of the moon?

Stewart.
August 19, 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
August 19, 2010
Rory Mcguire <rjmcguire@gm_no_ail.com> 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?

No. The same string in different object files may be different instances,
as may of course those in dynamically linked libraries. I would think the
optimizer feels free to move string literals around as it sees fit, and
the spec does not anywhere state that the compiler should merge string
literals.

-- 
Simen
August 19, 2010
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?

This should be expected but I wouldn't rely upon it.
August 20, 2010
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?


Interesting thanks guys.

Was just curious about the speed of comparisons for string literals. Because I believe the string comparisons check if a string "is" another string first.