Memory allocation in D (noob question)

Regan Heath regan at netmail.co.nz
Thu Dec 6 01:20:18 PST 2007


Steven Schveighoffer wrote:
> "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...

To me, all the behaviour is "normal" ;)

I think you're right about the reason it copies in this case.  I wonder 
if the solution is for the GC to keep a seperate list of memory blocks 
which are invariant... then on reallocate it simply ignores this list - 
resulting in the same behavior as the string literal case.

Regan



More information about the Digitalmars-d mailing list