Array expanding

Jonathan M Davis jmdavisProg at gmx.com
Sun Dec 18 15:42:31 PST 2011


On Sunday, December 18, 2011 22:12:07 RenatoL wrote:
> Reading the book from Alexandrescu we can find this (page 103-104,
> at least in my edition):
> 
> Expanding arrays has a couple of subtleties that concern possible
> reallocation of the array. Consider:
> 
> auto a = [87, 40, 10, 2];
> auto b = a; // Now a and b refer to the same chunk
> a ~= [5, 17]; // Append to a
> a[0] = 15; // Modify a[0]
> assert(b[0] == 15); // Pass or fail?
> 
> it seems natural that the test is ok but it is not... if we read
> more we find:
> "D leaves~= the freedom of either expanding by reallocation or
> opportunistically expanding in place if there is enough unused
> memory at the end of the current array."
> 
> At a first glance this seems to be a serious issue... it seems hard
> to accept that b can lost its connection to "a" in a silent mode due
> to a reallocation
> 
> Is there a safe mode to do this for large arrays?

Read this: http://www.dsource.org/projects/dcollections/wiki/ArrayArticle

It's the best article that there is on arrays and slices and how they work in 
D.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list