I have overloaded the new
and delete
operators to track where we allocate and deallocate memory. The overloaded new
operator works fine but I get an error when I try to use the overloaded delete
operator. I'm hoping someone can shed some lights. It's probably something minor.
Header File Code
void *operator new[] (size_t size, const char *file, int line, const char *function);
void operator delete(void *p, const char *file, int line, const char *function);
// other operators
#define NewWithDebug new (__FILE__, __LINE__, __FUNCTION__)
#define DeleteWithDebug delete (__FILE__, __LINE__, __FUNCTION__)
Source File Code
void *operator new (size_t size, const char *file, int line, const char *function)
{
printf("Memory Allocated (Size %zu): file= %s , function = %s , line =%d \n", size, file, function, line );
return malloc(size);
}
void *operator new[] (size_t size, const char *file, int line, const char *function)
{
printf ("Memory Allocated (Size %zu): file= %s , function = %s , line =%d \n", size, file, function, line);
return malloc(size);
}
void operator delete(void *p, const char *file, int line, const char *function)
{
printf("Memory Deallocated: file= %s , function = %s , line =%d \n", file, function, line);
free(p);
}
Main
int* Numbers = NewWithDebug int(5);
DeleteWithDebug Numbers; // <---- Error Here;
Error Message
error: expected `;' before 'Numbers