4 ms·
Arrays store just the pointers to their values, storing them inline isn't supported, like so: ptr to array --> array header
by YorickPeterse 3y ago
Arrays store just the pointers to their values, storing them inline isn't
supported, like so:
ptr to array --> array
header
size
capacity
[
val1 ptr --> val1
val2 ptr --> val2
]
At some point I'd like to support stack allocated values as an optimization,
including the ability to allocate values directly into arrays, but that's not a
priority at this point.
- tubthumper8 3y agoHmm I wasn't talking about the values in the array, whether those are pointers or Int or whatever doesn't matter, but the allocation of the array data itself. If the collection has capacity=2 (2 words) and another element is pushed in, typically you'd double the capacity, allocate new memory (4 words), copy the data over, and deallocate the old data. If the square brackets in your diagram actually represents another pointer then I think we're on the same page, but otherwise I don't see how the data could be allocated in the same chunk as the header/size/capacity if there can be multiple (potentially mutable) references. (hopefully the formatting on this works) ptr -> header size capacity data ptr -> [ val1 val2 ]
- YorickPeterse 3y agoAh gotcha. Yes, the actual layout of arrays is: header size capacity data ptr Where `data ptr` is a raw pointer to data that is realloc'd as the array grows.