Prevent slices from referencing old array after realocation?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 27 05:29:34 PDT 2015


On 5/27/15 1:55 AM, Gr8Ape wrote:
> I'm writing a program that handles lots of slices of larger
> arrays. I need the data of each of them all together for one
> operation, but otherwise it's more convenient to handle the
> elements in small groups. I could just store a pointer to my
> array, an offset, and a length but since I'm using D I'd much
> prefer to use slices...
>
> So I was looking through the documentation on slices and found
> out that if the array has to realocate then slices pointing to
> that array will keep on pointing to that block of memory, rather
> than the new location. Given that I'll be working with arrays
> that can vary wildly in size and that giving all of them the
> maximum capacity so they never realocate would be extremely
> inefficent, I need a workaround.
>
> So I'm wondering if there's a way to tell all the slices of an
> underlying array to now point to the new underlying array upon
> reallocation? Or would I have to iterate through and reassign
> every slice?

Slices can't do this, because they have no idea about each other. So 
when you append to one slice, you can't have it update all other 
relevant slices.

However, you could create a type that does this.

-Steve


More information about the Digitalmars-d-learn mailing list