I understand we can use Class-specific overloads of operator new[]/delete[] to specify our own memory allocation strategy.
So I write the following code. I barely want to use the held
field to record how much memory of a type X<id>
is allocated on heap.
However, I meet the problem that in operator new[]
, I can get the count
, while I can’t in operator delete[]
. So in operator delete[]
, I don’t know how much memory is actually released.
#include <cstdio>
#include <string>
#include <vector>
template <int Id>
struct X {
static inline int held = 0;
static void* operator new(std::size_t count)
{
held += count;
return ::operator new(count);
}
static void* operator new[](std::size_t count)
{
held += count;
return ::operator new[](count);
}
static void operator delete(void* ptr)
{
held -= sizeof(X<Id>);
::operator delete(ptr);
}
static void operator delete[] (void* ptr)
{
// This is not correct.
// However, I don't know the count.
held -= sizeof(X<Id>);
::operator delete [] (ptr);
}
std::string s1;
std::vector<int> v1;
};
int main() {
auto * dx2 = new X<1>[100]();
printf("x1d2 %dn", X<1>::held);
delete [] dx2;
printf("x1d2 released %dn", X<1>::held); // exptected 0
}
I understand one way is that I can allocate an extra bytes to store the count, it introduces extra overhead though.
Moreover, the compiler will actually store the count
somewhere when compiling operator delete []
, however, it is not exposed to users. I think I am now allowed to access the count
.