Reputation: 5521
Stated that there is no C++ equivalent of the C realloc
function, I've found in another question that such a thing is automatically managed by std::vector
and we should use it instead.
I'm fine with it. I guess that, since there is no other way of do reallocation, the std::vector
will just call realloc
for me.
However, the question is: if I'm overriding the new
and the delete
operators for managing of tracking memory usage globally, they will not be called in the case someone calls old C functions (malloc
, calloc
, realloc
, free
).
How can it be done? Is it correct that std::vector
replaces realloc
?
Upvotes: 0
Views: 254
Reputation: 254621
std::vector
won't call realloc
; it will use its allocator to achieve something similar: allocating new memory, moving objects into it, then deallocating the old memory. The default allocator uses operator new
and operator delete
, and so will use your replacements if you provide them.
realloc
would be entirely the wrong thing to do if the vector contains non-trivial objects; it copies raw data, while C++ objects generally have to be copied or moved by calling their special functions.
Nothing in the C++ library (except perhaps the default implementations of operator new
and operator delete
) will call the C allocation functions directly. Since you shouldn't be calling them yourself, you only need to worry about them if you're using a C library.
Upvotes: 3