4 ms·
Unfortunately, C++ containers generally cannot use realloc, because they are required to invoke their elements' move/copy constructors when their addresses chan
by Rusky 2y ago
Unfortunately, C++ containers generally cannot use realloc, because they are required to invoke their elements' move/copy constructors when their addresses change.
- menaerus 2y agoYes, you're correct that generally you would not be able to use realloc for reasons you mentioned (and also because of destructors) but for simple POD (trivially-constructible) types I think it should be possible, no? In that case, realloc will either (1) grow the existing memory, perhaps default initialize it, and call it a day (2) Or if growing the memory is not possible, (a) allocate a new region of memory, (b) memcpy the contents from the old block and (c) free the old block. Perhaps I am missing something but I think those are all the guarantees that realloc is giving to you.