Memory allocation in D (noob question)

Regan Heath regan at netmail.co.nz
Thu Dec 6 01:17:34 PST 2007


Matti Niemenmaa wrote:
> Derek Parnell wrote:
>> 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.
> 
> It's probably just a side effect of the fact that string literals are immutable.
> The compiler knows that it has to reallocate when appending to it, I guess?

I suspect you're right.  I think the reason is that a string literal is 
not allocated in the same way as the result from idup and maybe does not 
appear in the GC's list of memory blocks.  So, if the GC doesn't know 
about it, the GC does not reallocate but instead copies.

Regan



More information about the Digitalmars-d mailing list