string literal error?

Simen Kjaeraas simen.kjaras at gmail.com
Thu May 8 15:34:53 PDT 2008


badmadevil <badmadevil at gmail.com> wrote:

> I see, .reverse is buggy in D2 situation.
>
> How about D1?
> string in D1 is just alias of char[]. It should be valid in the case
> of x.reverse. (x is of type char[])
>
> It seems that the reason x,y,z reference to same string literal is to
> reduce bloated string data in the .exe (repeated literal array of other
> type, eg. integer, may not be reduced).
> dup/idup each literal array may avoid such bug, but would it be handy
> that idup/dup be implictly called during runtime?

Consider the case where you have a 2MB string literal (for some weird
reason), with 1K references to it. Would you like for the program to call
.dup on each of those? A bit hypothetical, I know, but it should highlight
the problem. I believe for D1, COW (copy on write) is how things should
work. In other words, the correct D1 program would look like this:

   string x = "bug?";
   string y = "bug?";
   string z = "bug?".dup.reverse; // copy because we're changing it
   writefln(x,y,z);
   x.dup.reverse; // copy because we're changing it
   writefln(x,y,z);
   writefln(typeof("bug?").stringof);

In the context of D1, I can see how non-duped string literals may be a
source of bugs, indeed. This (except that it does not work :p) is one of
the reasons I like D2's invariant strings.

-- Simen


More information about the Digitalmars-d-learn mailing list