Why doesn't std::vector use realloc?Do Sepples programmers ever profile their code?
The problem lies with reallocs interface.When calling realloc on memory that cannot be expanded without moving the data, it allocates new memory and memcopies the data to the newly allocated buffer.This is a problem, because if you want to move a variable in C++, you need to call the move constructor, since a variable might have a pointer to itself or one of its members (e.g. small vector optimization).
>>106995153You should never use realloc. The best solution is to use large enough chunks an linear growing, it will decrease the performance because of data locality but it can guarantee the elements in the array will be always in the same memory position.