unsafe toString() behavior

kris foo at bar.com
Mon Jun 26 23:36:16 PDT 2006


BCS wrote:
> In article <op.tbr0mdsfpo9bzi at moe>, Chris Miller says...
> 
>>On Mon, 26 Jun 2006 19:18:15 -0400, lenfestey at geemail.com  =
>>
>><lenfestey_member at pathlink.com> wrote:
>>
>>
>>>import std.stdio;
>>>import std.string;
>>>
>>>void main() {
>>>char[] x =3D toString(0);
>>>
>>>// la de da...
>>>x[0] =3D 'a';
>>>
>>>// guess what i print
>>>writefln("x=3D%s", toString(0));
>>>}
>>>
>>>It is because toString(uint) sometimes returns an array slice.
>>>
>>>Aaron Lenfestey
>>
>>In D you have to practice COW (copy-on-write) unless you are certain you
>>are the only one using the memory.
> 
> 
> IM(h)O toString should *always* return a copy.


But this particular issue is not so hard to resolve ... for instance, if 
a target-array was provided as part of the itoa() operation, there would 
be no need for heap activity or for aliasing. On the flip side, it would 
mean the method signatures are somwhat less succinct? Perhaps there are 
other detrimental aspects?



More information about the Digitalmars-d-bugs mailing list