Hi there.
I have a situation where I would like to monitor all memory allocation and de-allocation, something like the following:
Code:
void operator delete(void *p)
{
cout << "Number of bytes de-allocated: " << [Number of bytes];
free(p);
}
//---------------------------------------------------------------------------
void* operator new(size_t s)
{
cout << "Number of bytes allocated: " << s;
void* p = malloc(size);
if (p == 0)
{
throw std::bad_alloc();
}
return p;
}
//---------------------------------------------------------------------------
void operator delete[](void *p)
{
cout << "Number of bytes de-allocated: " << [Number of Bytes];
free(p);
}
//---------------------------------------------------------------------------
void* operator new[](size_t s)
{
cout << "Number of bytes allocated: " << s;
void* p = malloc(size);
if (p == 0)
{
throw std::bad_alloc();
}
return p;
}
//---------------------------------------------------------------------------
int main()
{
const int s = 5;
int* p = new int [s];
delete [] p;
return 0;
}
Can anyone tell me if it is at all possible to obtain the size of the block of memory to be freed?
Please excuse any typos or anything, I typed this straight into the board. I'm sure my intentions are quite clear.
Thank you.