unsafe toString() behavior

Regan Heath regan at netwin.co.nz
Mon Jun 26 16:26:09 PDT 2006


On Mon, 26 Jun 2006 23:18:15 +0000 (UTC), lenfestey at geemail.com  
<lenfestey_member at pathlink.com> wrote:
> import std.stdio;
> import std.string;
>
> void main() {
> char[] x = toString(0);
>
> // la de da...
> x[0] = 'a';
>
> // guess what i print
> writefln("x=%s", toString(0));
> }
>
> It is because toString(uint) sometimes returns an array slice.

Theoretically "Copy on Write" should be used before writing to the string.  
Ideally the write should be detected and prevented by the compiler.  
Eventually I hope Walter can come up with a "const that works" for D :)

Regan



More information about the Digitalmars-d-bugs mailing list