The code below allows you to see size of variable being deleted:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct P {
static void operator delete(void* ptr, std::size_t sz)
{
cout << "custom delete for size " << sz <<endl;
delete (ptr); // ::operator delete(ptr) can also be used
}
static void operator delete[](void* ptr, std::size_t sz)
{
cout << "custom delete for size " << sz <<endl;
delete (ptr); // ::operator delete(ptr) can also be used
}
};
int main()
{
P* var1 = new P;
delete var1;
P* var2 = new P[10];
delete[] var2;
}
Output:
custom delete for size 1
custom delete for size 14
My questions is:
Where does argument std::size_t sz
get assigned value?
#include <bits/stdc++.h>
... and when you combine it withusing namespace std;
, you can cause some pretty impressive unforced errors. Best to just not use either and avoid unnecessary trainwrecks entirely. – Greenbrier