Array operations and 'Copy on Whatever'

Frank Benoit keinfarbton at nospam.xyz
Thu Aug 3 16:27:57 PDT 2006


Serg Kovrov schrieb:

> In particular, did array concatenation (~, ~=) make a copy of right
> operand?
> 

~ allocates a new array which can hold both operands. See
http://www.digitalmars.com/d/operatoroverloading.html for overloading opCat.

~= does allocate new memory only of the left-side is not big enough.

The right side is always copied.

> Consider this example:
>> char[][] array_of_strings;
>>
>> for (int i = 0; i < 10; i++)
>> {
>>     char[] tmp = format("this is %d", i);
>>     array_of_strings ~= tmp;
>>     delete tmp; // <-- here
>> }
>>
>> foreach (char[] str; array_of_strings)
>>     writefln("%s", str);
> 
> When I delete tmp, strings in array_of_strings being trashed.
> 

You concat to a char[][], which is an array of char[].
An array is a struct of a pointer and an integer for the length.

array_of_strings ~= tmp;

copies the tmp array struct to the end of array_of_strings. Perhaps
array_of_strings was new allocated and copied. But you did not copy the
characters of tmp. The appended array still points to where tmp also
points to.

With delete tmp, you delete the characters. Here is the problem.

> But on the other hand, in more simple example:
>> char[] s1 = "one";
>> char[] s2 = "two";
>> s1 ~= s2;
>> delete s2;
> 
> Deleting s2 will not affect s1.
> 

Here you append to char[] -- not char[][].
s1 ~= s2;
does not copy the characters. Deleting s2 is now no problem.







More information about the Digitalmars-d-learn mailing list