This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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?





 
 
Thanks&Regards,
Fan Xiaohua


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]