www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - games with toString()

reply "Kris" <fu bar.com> writes:
toString(int) returns a shared, global slice for small numbers:

import std.string;
import std.stdio;

void main()
{
        char[] c = toString(1);
        writefln (c);
        c[0] = '!';
        writefln (toString(1));
}

emits:

1
!

Read-only arrays would resolve this, and a whole lot more besides  :-)
Dec 10 2005
next sibling parent JT <JT_member pathlink.com> writes:
In article <dng17i$6n6$1 digitaldaemon.com>, Kris says...
toString(int) returns a shared, global slice for small numbers:

import std.string;
import std.stdio;

void main()
{
        char[] c = toString(1);
        writefln (c);
        c[0] = '!';
        writefln (toString(1));
}

emits:

1
!

Read-only arrays would resolve this, and a whole lot more besides  :-)
WOOOOOOW! ok yeah Ive had weird problems like this and Im learning more every day about how D handles these things. Thanks very much for pointing this out - ive got .dups sprinkled all over my code becuase Im still trying to figure out what these problems are - but you just gave me some important information. Its quite straightforward but D is certainly a new paradigm one has to get used to.
Dec 11 2005
prev sibling parent reply Sean Kelly <sean f4.ca> writes:
Kris wrote:
 toString(int) returns a shared, global slice for small numbers:
 
 import std.string;
 import std.stdio;
 
 void main()
 {
         char[] c = toString(1);
         writefln (c);
         c[0] = '!';
         writefln (toString(1));
 }
 
 emits:
 
 1
 !
 
 Read-only arrays would resolve this, and a whole lot more besides  :-)
I suspect this is a const string and attempting the above on Linux would result in a run-time error. But it's a good point nevertheless :-) Sean
Dec 11 2005
parent "Kris" <fu bar.com> writes:
"Sean Kelly" <sean f4.ca> wrote
 Kris wrote:
 Read-only arrays would resolve this, and a whole lot more besides  :-)
I suspect this is a const string and attempting the above on Linux would result in a run-time error. But it's a good point nevertheless :-)
You just gave me an idea ;-)
Dec 11 2005