digitalmars.D.learn - String literals have only one instance?
- Rory Mcguire <rjmcguire gm_no_ail.com> Aug 19 2010
- Jonathan Davis <jmdavisprog gmail.com> Aug 19 2010
- Stewart Gordon <smjg_1998 yahoo.com> Aug 19 2010
- bearophile <bearophileHUGS lycos.com> Aug 19 2010
- Johannes Pfau <spam example.com> Aug 19 2010
- "Simen kjaeraas" <simen.kjaras gmail.com> Aug 19 2010
- Sean Kelly <sean invisibleduck.org> Aug 19 2010
- Rory Mcguire <rjmcguire gm_no_ail.com> Aug 20 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?
Aug 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.
Aug 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.
Aug 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
Aug 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?
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
Aug 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
Aug 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.
Aug 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?
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.
Aug 20 2010









Stewart Gordon <smjg_1998 yahoo.com> 