Slicing betterC
Adam D. Ruppe
destructionator at gmail.com
Thu Sep 6 17:34:18 UTC 2018
On Thursday, 6 September 2018 at 17:10:49 UTC, Oleksii wrote:
> struct Slice(T) {
> size_t capacity;
> size_t size;
> T* memory;
> }
There's no capacity in the slice, that is stored as part of the
GC block, which it looks up with the help of RTTI, thus the
TypeInfo reference.
Slices *just* know their size and their memory pointer. They
don't know how they were allocated and don't know what's beyond
their bounds or how to grow their bounds. This needs to be
managed elsewhere.
If you malloc a slice in regular D, the capacity will be returned
as 0 - the GC doesn't know anything about it. Any attempt to
append to it will allocate a whole new block.
In -betterC, there is no GC to look up at all, and thus it has
nowhere to look. You'll have to make your own struct that stores
capacity if you need it.
I like to do something like
struct MyArray {
T* rawPointer;
int capacity;
int currentLength;
// most user interaction will occur through this
T[] opSlice() { return rawPointer[0 .. currentLength]; }
// fill in other operators as needed
}
More information about the Digitalmars-d-learn
mailing list