-2

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

Programmer
  • 15
  • 5
  • You are not passing the parameter Numbers to delete () – drescherjm Mar 27 '19 at 17:35
  • If I do it like this```DeleteWithDebug (Numbers); ```, I get this error, ```error: '(0, __FUNCTION__)' cannot be used as a function``` – Programmer Mar 27 '19 at 17:39
  • That is why I deleted my second comment. I thought about it for a few seconds and saw my mistake. – drescherjm Mar 27 '19 at 17:40
  • BTW, You are not calling the constructor / destructor in your new / delete. you probably want to use ::new and ::delete instead of malloc() / free. – drescherjm Mar 27 '19 at 17:53
  • Related: https://stackoverflow.com/questions/438515/how-to-track-memory-allocations-in-c-especially-new-delete – drescherjm Mar 27 '19 at 18:00

1 Answers1

0

Your problem is that you're not allowed to use a placement delete operator with a delete expression: see here. They are only called when a placement new expression encounters an exception in the constructor.

From the above

If the constructor throws an exception, it is necessary to deallocate that storage before propagating the exception back to the code that executed the placement new expression, and that is the purpose of the placement delete functions.

JMAA
  • 1,730
  • 13
  • 24
  • So you are saying I can't do something like this? ```void operator delete(void *p, const char *file, int line, const char *function);```, I really wanted to track the deallocation in the code. Any other ways? – Programmer Mar 27 '19 at 17:45
  • To achieve what you want, you don't need to overload the operators, since you're having to use macros anyway. Just write a functional macro that inserts your debug prints before doing the actual `new` or `delete` – JMAA Mar 27 '19 at 21:25