In this library there are the following global overrides for new, delete, new[] and delete[]:
/* ------------------------------------------------------------ */
/* ---------------------- new and delete ---------------------- */
/* ------------------------------------------------------------ */
/* ---------------------------------------- operator new */
void *operator new(size_t size)
{
void *p = MemTrack::TrackMalloc(size);
if (p == NULL) throw std::bad_alloc();
return p;
}
/* ---------------------------------------- operator delete */
void operator delete(void *p)
{
MemTrack::TrackFree(p);
}
/* ---------------------------------------- operator new[] */
void *operator new[](size_t size)
{
void *p = MemTrack::TrackMalloc(size);
if (p == NULL) throw std::bad_alloc();
return p;
}
/* ---------------------------------------- operator delete[] */
void operator delete[](void *p)
{
MemTrack::TrackFree(p);
}
The overrides for new and delete make sense to me. But what about new[] and delete[]? AFAIK in c++ when a new array is created using new[], it also stores the number of elements before the pointer, so that delete[] can call the destructors on the elements. Does this need to be considered when overriding new[] and delete[] or is there some kind of implicit "wrapper" that adds that on the outside of whatever the overriding implementation allocates?
If not, how is it done properly? it seems the new[] operator also only gets the number of bytes that should be allocated, and not the number of elements.