5 ms·
Interesting idea but how does C++ guarantee contiguous memory for a vector? I just don’t see how a data structure with an arbitrary, dynamic size can also resid
by anthomtb 4y ago
Interesting idea but how does C++ guarantee contiguous memory for a vector? I just don’t see how a data structure with an arbitrary, dynamic size can also reside in a contiguous range of address space.
- fnbr 4y agoWhen the array is resized, it’s moved to a new contiguous block of memory: everything is copied or moved over. See: https://stackoverflow.com/questions/8261037/what-happen-to-pointers-when-vectors-need-more-memory-and-realocate-memory https://stackoverflow.com/questions/8261037/what-happen-to-p...
- frankchn 4y agoSimple, you just allocate a bigger contiguous chunk of memory and copy the entire vector over when the current chunk maxes out.
- zabzonk 4y agothe size of a c++ object is fixed at compile-time