I am having a problem with the placement new operator. I have two programs: Program1 (operator.cpp) and Program2 (main.cpp): Program1: operator.cpp
void *operator new(size_t size)
{
void *p;
cout << "From normal new" << endl;
p=malloc(size);
return p;
}
void *operator new(size_t size, int *p) throw()
{
cout << "From placement new" << endl;
return p;
}
Here is the second program to which the first is linked: main.cpp:
#include <new>
int main()
{
int *ptr=new int;
int *ptr1=new(ptr) int(10);
}
I am individually compiling operator.cpp and main.cpp as shown:
operator.cpp: g++ -g -c -o operator operator.cpp
Then linking it with main.cpp:
g++ -g -o target operator main.cpp.
Surprisingly, when i am executing "./target" it is printing: "From normal new". The expected output is: From normal new From placement new.
However, if the placement new and the main are put in the same file itself, then the output is as expected: From normal new, From placement new
Can anyone please help me regarding this? This stuff is related to my official work and is very very urgent.