Memory allocation in D (noob question)

Steven Schveighoffer schveiguy at yahoo.com
Wed Dec 5 08:43:46 PST 2007


"Regan Heath" wrote
> 2. It could be argued that "concatenation" is the x ~ y form and not the 
> ~= form, which is called "append".  From the website spec:
>
> "The binary operator ~ is the cat operator. It is used to concatenate 
> arrays"
>
> "Similarly, the ~= operator means append"
>
> "Concatenation always creates a copy of its operands, even if one of the 
> operands is a 0 length array"

Look at the example for append:

"a ~= b;		// a becomes the concatenation of a and b"

This is the only explanation of what "append" does.

Yes, we are splitting hairs, but they are important hairs to split :) 
Having the spec be accurate is important for not only compiler implementors 
(which right now doesn't matter much but might in the future) and to 
developers using D.

Just a simple explanation of:

append may or may not re-use the memory that the original array uses. 
Therefore you should not use the append operator unless you know the array 
to be appended to is a dynamic array and not a slice of a dynamic array.  If 
this isn't the case, memory corruption can occur:

(paste Oskar's example here)

-Steve 





More information about the Digitalmars-d mailing list