Prevent slices from referencing old array after realocation?

Gr8Ape via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 27 00:55:28 PDT 2015


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?


More information about the Digitalmars-d-learn mailing list