Answer the question
In order to leave comments, you need to log in
Overriding operator delete [] for a class. Glitch?
Good afternoon, ladies and gentlemen.
It was required to replace a heap with "custom" in the project.
I'm reading on cppreference about the delete [] operator . There is this code:
#include <iostream>
// sized class-specific deallocation functions
struct X {
static void operator delete(void* ptr, std::size_t sz)
{
std::cout << "custom delete for size " << sz << '\n';
::operator delete(ptr);
}
static void operator delete[](void* ptr, std::size_t sz)
{
std::cout << "custom delete for size " << sz << '\n';
::operator delete(ptr);
}
};
int main() {
X* p1 = new X;
delete p1;
X* p2 = new X[10];
delete[] p2;
}
Possible output:
custom delete for size 1
custom delete for size 18
custom delete for size 1
custom delete for size 1
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question