In C ++, you can save the global std::map<void*, std::size_t>
to track the size of each selected block; your own distribution function will register the size during distribution, and the release function will delete the record. (Update: either do as the related question is asked, and allocate a little more memory and save the size there.)
A more fundamental problem is that it will probably be very limitedly used in a typical C ++ program: allocations there are performed mainly in two ways: 1) through explicit new
expressions that call ::operator new()
, which in ( usually) calls malloc()
and 2) through std::allocator<T>::allocate()
, which is implemented in many platforms in terms of ::operator new()
.
The problem is that you do not have control over the features of your platform. You can replace the global operator with a new one to use your own MyMalloc()
, but by default std::allocator
can use malloc()
directly and therefore cannot be affected by this.
A cleaner approach for debugging is to use an external tool like valgrind
to track heap usage. For ongoing internal use, tracking placement sizes will also result in significant performance degradation.
source share