Memory allocation in D (noob question)
Steven Schveighoffer
schveiguy at yahoo.com
Wed Dec 5 13:53:31 PST 2007
"Derek Parnell" wrote
> On Wed, 05 Dec 2007 16:18:46 +0000, Regan Heath wrote:
>
>> [example pasted again for clarity]
>>
>> > string ab = "ab".idup;
>> > string a = ab[0..1];
>> > a ~= "c";
>> > writefln("ab = ",ab); // also outputs "ac"
>
> However, this is fine ...
>
> string ab = "ab";
> string a = ab[0..1];
> a ~= "c";
> writefln("ab = ",ab); // outputs "ab"
> writefln("a = ",a); // outputs "ac"
>
> So it seems that the '.idup' property is affecting things.
Yes, I noticed that too. However, it's simply the non-deterministic
behavior of the ~= operator that is causing this. For literal strings, I
suspect they are not allocated by the GC, and so the GC can't extend them,
so the normal behavior kicks in. But idup is supposed to give me an
invariant array. The code is still changing invariant data...
-Steve
More information about the Digitalmars-d
mailing list