RE: why my overloaded operate new is called?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 
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@xxxxxxxxx] 
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@xxxxxxxxxxx'
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?


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux