Appending to a dynamic array at specific offset

Kevin Bealer kevinbealer at gmail.com
Thu Jan 25 23:25:57 PST 2007


Derek Parnell wrote:
> On Mon, 22 Jan 2007 22:34:51 -0600, Joseph Bell wrote:
...


I think these two:

>> msg.length = 3;
>> msg ~= data; // Effectively resize the array back to 3 and then append

and

>  msg.length = data.length + 3;
>  msg[3..$] = data;

Are identical in practice, except that the second does not touch the 
length unless it has to change.  But as far as reallocation and other 
criteria, I don't think there is a difference; D knows how big the msg[] 
block is, so it can do the "~=" or "msg.length=..." in the efficient way 
in either case.

I think it also will not always realloc for a larger size, because the I 
think the underlying implementation does "capacity" doubling, something 
like the way C++'s vector is normally written to do.

Kevin



More information about the Digitalmars-d mailing list