HeapBlock and realloc


#1

Bit of a C++ question here, is there any situation where a call to realloc will fail but an immediate call to malloc will succeed? I know on modern systems memory allocation isn’t usually a problem but in this situation the buffer may be used to hold very large sets of data.

Looking at this realloc reference if realloc can’t simply extend the existing block it will return a new block. In HeapBlock::realloc where does this old block get freed? Wouldn’t we need something like:

void* temp = data; data = std::realloc (data, newNumElements * elementSize) if (temp != data) std::free (temp);

Or am I missing something fundamental?


#2

No - if realloc needs to return a new pointer, it will also delete the old one for you. No need to do it manually.