why my overloaded operate new is called?
Fan Xiaohua-A18426
a18426@motorola.com
Fri Apr 14 01:19:00 GMT 2006
Hi, Eljay,
Thanks for your quick response!
But why my overloaded placement new "void* operator new(size_t size, int* memHdr)" is not called?
Thanks&Regards,
Tina
-----Original Message-----
From: John Love-Jensen [mailto:eljay@adobe.com]
Sent: 2006年4月13日 20:37
To: Fan Xiaohua-A18426; MSX to GCC
Subject: Re: why my overloaded operate new is called?
Hi Fan,
> why my first overloaded operator new is called since #include
> "utilnew.h" is not added in mymain.cc?
Your new operator is a function.
You are linking against your function.
Hence, you are utilizing your function, rather than the one that would otherwise be used.
> Is this related with certain compile option?
No.
HTH,
--Eljay
-----Original Message-----
From: Fan Xiaohua-A18426
Sent: 2006年4月13日 15:21
To: 'gcc-help@gcc.gnu.org'
Subject: why my overloaded operate new is called?
Hello, all
Sorry for disturbing all of you, I'm not sure whether I have sent my question to a right mail list.
I have one c++ file named utilnew.cc:
#include <iostream>
using namespace std;
// ........overload new.delete...
void* operator new(size_t size)
{
cout << "enter my new1\n";
return malloc(size);
}
void operator delete(void* memoryPtr)
{
cout << "enter mydelete1\n";
free(memoryPtr);
}
void* operator new(size_t size, int* memHdr) {
cout << "into my new2\n";
return malloc(size);
}
void operator delete(void* memoryPtr, int*) {
printf("enter mydelete2\n");
}
and one header file utilnew.h
void* operator new(size_t size);
void operator delete(void* memoryPtr);
void* operator new(size_t size, int* memHdr); void operator delete(void* memoryPtr, int*); now I create a test c++ file named mymain.cc for test #include <iostream>
using namespace std;
int main()
{
int *temp = new int(1);
int *p = new(temp)int;
cout << "temp is :" << temp <<"\n";
cout << "p is :" << p << "\n";
delete temp;
return 0;
}
I compile them:
g++ -o mymain mymain.cc utilnew.cc
Then run mymain
I get the following result:
enter my new1
temp is :0x9489008
p is :0x9489008
enter mydelete1
My question is:
why my first overloaded operator new is called since #include "utilnew.h" is not added in mymain.cc? Is this related with certain compile option?
More information about the Gcc-help
mailing list