Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Hmm 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 ]


Ah 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: