String literals have only one instance?

Simen kjaeraas simen.kjaras at gmail.com
Thu Aug 19 06:58:29 PDT 2010


Rory Mcguire <rjmcguire at 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


More information about the Digitalmars-d-learn mailing list