[Issue 2418] Same-value string (char[]) literals get overwritten (unlike array literals)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Oct 14 18:34:41 PDT 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2418


business3 at twistedpairgaming.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




------- Comment #3 from business3 at twistedpairgaming.com  2008-10-14 20:34 -------
(In reply to comment #2)
> The bug is at your site. You don't make string copies and work on a single
> string.
> 
> char[] a = "AAA";
> char[] b = a; // both point to the same location
> a[0] = '!';
> assert(b[0] == '!'); // should be true since they both point to the same
> location
> 
> Same example in C (this may help understand better):
> char* str = "AAA";
> char* ptr = str;
> str[0] = '!';
> assert(ptr[0] == '!');
> 

I don't think that's applicable (or at least, it shouldn't be). Here's a
shortened example:

char[] a = "AAA";
a[0] = '!';
char[] b = "AAA";

It's absurd that 'b' should become "!AA". It's clear that the compiler is
"optimizing" both "AAA" string literals into the same reference:

char[] _tmp1 = "AAA";
char[] a = _tmp1;
a[0] = '!';
char[] b = _tmp1;

But I'm saying that transformation should not be occurring. The two "AAA"
literals should not share the same reference. In D2, array literals are
immutable, so yes, in D2 it works out fine. But in D1, array literals are *not*
immutable, so the compiler shouldn't assume that array literals with a equal
*value* can safely share the same *reference*.

> The actual bug is that the following line should not compile:
> char[] a = "AAA";

Again, this is for D1, so the idea of strings being immutable doesn't apply.

Besides, at the very least there's still an inconsistency:

char[] a = "AAA";
a[0] = '!';
char[] b = "AAA";  // b is "!AA"

char[] c = ['A', 'A', 'A'];
c[0] = '!';
char[] d = ['A', 'A', 'A'];  // d is "AAA"

The above shows that two occurrences of "AAA" share the same reference, but two
occurrences of ['A', 'A', 'A'] have separate references. The former leads to
odd side-effects, the latter does not.


-- 



More information about the Digitalmars-d-bugs mailing list