4 ms·
> I share a dislike for std::vector type automatically reallocating arrays -- because of the "reallocating" part. I like preallocated chunks and stable pointers
by Veliladon 2y ago
> I share a dislike for std::vector type automatically reallocating arrays -- because of the "reallocating" part. I like preallocated chunks and stable pointers.
Use request at creation to set a capacity for 99.9% of use cases. Still have realloc there to stop any sneaky buffer overflows.
Have your cake and eat it too!
- jstimpfle 2y agoYou can't just realloc you way out of your buffer overflow, when this will invalidate existing references to members in the old allocation.
- MontagFTB 2y agostd::deque doesn’t invalidate existing elements when new ones are added to either end of it. It’s not a panacea, and has its own set of tradeoffs, but if non-invalidation is a requirement for you it is available.
- ape4 2y agoIn that case if a realloc() happens won't it request double the existing size (ie 99.9% * 2) But if we had let the vector grow naturally it would probably not make such a big realloc()