digitalmars.D.bugs - games with toString()
- "Kris" <fu bar.com> Dec 10 2005
- JT <JT_member pathlink.com> Dec 11 2005
- Sean Kelly <sean f4.ca> Dec 11 2005
- "Kris" <fu bar.com> Dec 11 2005
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
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
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
"Sean Kelly" <sean f4.ca> wroteKris 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









JT <JT_member pathlink.com> 