Re: One question about operator new in libstdc++.

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

 



Hi Ian,
On Fri, Oct 15, 2010 at 11:08:48PM -0700, Ian Lance Taylor wrote:
> Shigang Zan <zanshigang@xxxxxxxxxxx> writes:
> 
> > Can I have a solution to override all classes' operator new and
> > delete? or we can only override for one class?
> 
> This is not a question about gcc.  It is a question about the C++
> language.
> 
> As far as I know there is no way to override the global operator new.
I'm not sure there: It is possible to override all the functions for
dynamic memory management defined in the header <new>.
This includes
void* operator new(std::size_t) throw (std::bad_alloc);
void* operator new[](std::size_t) throw (std::bad_alloc);

Now, as soon as one of those functions is overloaded, it is used (at
least by my gcc-version) throughout the code. At least that's my
impression (I haven't checked what is happening for e.g.  separate
compilation units, already compiled parts of the standard library e.g.):

#include <iostream>
int mem[1000];
int start;
void *operator new (std::size_t s){
  start +=s;
  return mem+start-s;
}
int main()
{
  start = 0;
  int *x = new int[10];
  std::cout << sizeof(int) * 10 << " " << start << std::endl;
}

That's also the way how I understand the standard (§17.4.3.4(2,3)): 

Axel


[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