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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Oct 14 15:50:59 PDT 2008


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

           Summary: Same-value string (char[]) literals get overwritten
                    (unlike array literals)
           Product: D
           Version: 1.035
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: business3 at twistedpairgaming.com


When a string is initialized with a string literal, and part of the string is
then changed, all other string literals with the same value reflect the same
change.

For the following code:

------------------
module test;
import tango.io.Stdout;

class FooChar
{
        char[] str;

        this(char[] _str)
        {
                this.str = _str;
        }
}

class FooInt
{
        int[] ints;

        this(int[] _ints)
        {
                this.ints = _ints;
        }
}

void main()
{
        FooChar a;
        FooChar b;
        FooChar c;
        FooChar d;

        a = new FooChar("AAA");
        Stdout.formatln("a.str: {}", a.str);
        a.str[0] = '!';
        Stdout.formatln("a.str: {}", a.str);
        a = new FooChar("AAA");
        Stdout.formatln("a.str: {}", a.str);
        Stdout.formatln("");

        b = new FooChar("AAA");
        Stdout.formatln("b.str: {}", b.str);
        b.str[0] = '!';
        Stdout.formatln("b.str: {}", b.str);
        b = new FooChar("AAA");
        Stdout.formatln("b.str: {}", b.str);
        Stdout.formatln("");

        c = new FooChar("AA1");
        Stdout.formatln("c.str: {}", c.str);
        c.str[0] = '!';
        Stdout.formatln("c.str: {}", c.str);
        c = new FooChar("AA1");
        Stdout.formatln("c.str: {}", c.str);
        Stdout.formatln("");

        const char[] dInit = "AA2";
        d = new FooChar(dInit);
        Stdout.formatln("d.str: {}", d.str);
        d.str[0] = '!';
        Stdout.formatln("d.str: {}", d.str);
        d = new FooChar(dInit);
        Stdout.formatln("d.str: {}", d.str);
        Stdout.formatln("");

        FooInt i;

        i = new FooInt([1, 2]);
        Stdout.formatln("i.ints: {}", i.ints);
        i.ints[0] = 77;
        Stdout.formatln("i.ints: {}", i.ints);
        i = new FooInt([1, 2]);
        Stdout.formatln("i.ints: {}", i.ints);
}
------------------

Expected output:
a.str: AAA
a.str: !AA
a.str: AAA

b.str: AAA
b.str: !AA
b.str: AAA

c.str: AA1
c.str: !A1
c.str: AA1

d.str: AA2
d.str: !A2
d.str: AA2

i.ints: [1, 2]
i.ints: [77, 2]
i.ints: [1, 2]

Actual output:
a.str: AAA
a.str: !AA
a.str: !AA

b.str: !AA
b.str: !AA
b.str: !AA

c.str: AA1
c.str: !A1
c.str: !A1

d.str: AA2
d.str: !A2
d.str: !A2

i.ints: [1, 2]
i.ints: [77, 2]
i.ints: [1, 2]

This might be a side-effect of the fix for #817:
http://d.puremagic.com/issues/show_bug.cgi?id=817


-- 



More information about the Digitalmars-d-bugs mailing list