Memory allocation in D (noob question)

Matti Niemenmaa see_signature at for.real.address
Wed Dec 5 13:55:05 PST 2007


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?

import std.stdio;
void main() {
	int[] ab = [0, 1];
	int[] a = ab[0..1];
	a ~= 2;
	writefln(ab);
	writefln(a);
}

-- 
E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi



More information about the Digitalmars-d mailing list