1

I want to overload 'new' operator. I made one Header file where macro for 'new' is declared.

HeaderNew.h

#ifndef MYNEW_H
#define MYNEW_H

#define  new    new(__FILE__, __LINE__)

void* operator new(std::size_t size, const char* file, unsigned int line);

#endif

myNew.cpp

#include<iostream>   
#include<malloc.h>
#include<cstddef>
#include "mynew.h"
using namespace std;

#undef      new

void* operator new(std::size_t size, const char* file, unsigned int line){
    void *ptr = malloc(size);
    cout << "This is overloaded new." << endl;
    cout << "File : " << file << endl;
    cout << "Line : " << line << endl;
    cout << "Size : " << size << endl;
    return ptr;
}

test.cpp

#include <iostream>
#include "mynew.h"
using namespace std;

int main()
{
   int * ptr1 = new int;
   cout << "Address : " << ptr1 << endl;
   //delete ptr1;
   return 0;
}

Here, I want to know the file name and line number of 'new' operator used in test.cpp . But i got a error as mentioned below.

error : declaration of ‘operator new’ as non-function in #define new new(FILE, LINE)

Can anyone tell me the reason for this error & its appropriate solution. Thanks in advance..:)

9
  • you are mixing up operator new and what is colloquially known as the new operator, the latter is a keyword built in the language and cannot (shouldn't) be redefined. The new operator calls operator new to allocate first the memory, then invokes the constructor of the passed object. Commented Mar 10, 2015 at 3:22
  • 2
    Even if that macro was legal wouldn't your function then expand to: void* operator new(__FILE__, __LINE__)(std::size_t size, const char* file, unsigned int line); ? Commented Mar 10, 2015 at 3:29
  • @vsoftco have you never heard of overloading new? Commented Mar 10, 2015 at 3:30
  • 1
    @immibis I heard about overloading operator new, not the keyword new. It seems the OP is re-defining a keyword: #define new new(__FILE__, __LINE__) which seems a sure recipe for disaster Commented Mar 10, 2015 at 3:31
  • @vsoftco Yes, he/she is using the preprocessor to replace new calls with his/her overloaded version that takes the file and line number. It's not portable, but in practice it will probably work, and it's not what the error is about. Apart from being a keyword and not a function, this is the same principle used by assert. Commented Mar 10, 2015 at 3:34

1 Answer 1

3

#defines work everywhere after the definition.

Which means this:

#define  new    new(__FILE__, __LINE__)
void* operator new(std::size_t size, const char* file, unsigned int line);

gets changed to this, by the preprocessor:

void* operator new(__FILE__, __LINE__)(std::size_t size, const char* file, unsigned int line);

See the problem?

Try moving the #define line after the declaration of operator new.

Also, be aware that this trick is not entirely robust - for example, it will break any other operator new declarations you have, or placement new calls (including in any standard headers that use placement new); "placement new" is a built-in overload of operator new.

Sign up to request clarification or add additional context in comments.

2 Comments

@immibis...thnks 4 the comment. But still its not working after moving the #define line after the decleration of operator new...:(..you can also check in ur system. Its giving the same error.
@immibis...thanks 4 the comment.But still its not working after moving the #define line after the declaration of operator new...:(..You can also check in your system...Its giving the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.