How do you remove/insert elements in a dynamic array without allocating?
Jonathan M Davis
jmdavisProg at gmx.com
Wed Nov 7 02:17:38 PST 2012
On Wednesday, November 07, 2012 10:45:46 monarch_dodra wrote:
> Also, Array uses a deterministic memory model, just like vector,
> that releases content as soon as it goes out of scope. I could
> have done without that, personally, but to each their own.
Yes. The built-in arrays work extremely well as they are designed, but they
are _not_ meant for cases where you need the array to have specific ownership.
It appears that Array is using malloc and free internally (including telling
the GC about it so that it's scanned for references/pointers as appropriate),
so it doesn't even have to worry about stuff like assumeSafeAppend. It's very
much in line with std::vector and what should be used if you want something
similar to std::vector.
By the way, monarch_dodra, since you've been messing around with Array
recently, I would point out that it looks like setting the length doesn't work
properly if you set it greater than the current length, let alone greater than
the current capacity. _capacity is not adjusted if newLength is greater than
it, and no calls to GC.removeRange or GC.addRange are made, so it doesn't look
like newly allocated memory is being tracked by the GC like it should if
length is allocating it. And I find the whole length vs capacity bit in Array
highly confusing, because it looks like the length of the payload is used for
length, when I would have expected the length of the payload to be the
capacity and for there to be a separate length for the actual number of
elements. So, I really don't know what's going on with std.array.Array's
implementation without studying it a lot more - hopefully you do. But at
minimimu, I'd advise verifying that the GC.addRange and GC.removeRange calls
are being correctly done, because regardless of what the deal is with capacity
vs length, it seems seriously off to me that no GC functions are called when a
realloc is made.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list