[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 16:01:17 PDT 2008


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


2korden at gmail.com changed:

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




------- Comment #2 from 2korden at gmail.com  2008-10-14 18:01 -------
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] == '!');

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

Problem is, D1 doesn't have invariant type modifier and the spec is frozen, so
this will never be fixed. D2, however, doesn't allow you to do this.

Solution would be to make copies explicitly:
a = new FooChar("AAA".dup);


-- 



More information about the Digitalmars-d-bugs mailing list