Re: Overriding the C++ Operator new
Re: Overriding the C++ Operator new
- Subject: Re: Overriding the C++ Operator new
- From: David Fang <email@hidden>
- Date: Tue, 27 Sep 2005 01:12:45 -0400 (EDT)
> Is it possible to replace operator new GLOBALLY, and still use other
> things in the standard C++ support library. I want to use the Boehn
> conservative garbage collector.
Hi,
I don't have a complete mastery of all the intricacies of the C++,
but here's a go (someone please correct me if I'm wrong!):
You can override the global new just by declaring operator new in the
global namespace. This will, as you said, override calls to new <class>
(except where classes have operator new overloaded inside them). The
standard library uses std::allocator (by default) to perform its
allocations. (You know, one of those default template arguments in STL
classes that you never have to pass?) If you dig through
/usr/include/gcc/darwin/<version>/c++/bits/stl_alloc.h, you'll see that it
just wraps calls to ::operator new(). You can always explicitly use
std::allocator, or pass in your own custom allocator to the STL containers
to break the dependency on global new.
To answer your question, though, what do you mean by "use other
things in std. C++"? By modifying global new, you'll be changing the
behavior of std::allocator, and everything that depends on it
(containers). (The rest of STL remains unaffected.) If you mean use both
your allocator and the original implementation of new/delete, then you
need to somehow specify where you want to use each allocator. How you'd
want to go about doing this depends on which case is less frequent, and
hence less painful to instrument your code with. To replace global new
and explicitly call the original implementation (and delete!) case-by-case
could prove painful and error-prone. You might want to consider
overloading new on a per class basis. With the help of some preprocessor
macros, this can be painless (been there, done that), even if it involves
all of your classes. The problem with this is that it wouldn't cover
allocation of POD (plain-old-data) types. *shrug* Can you give us some
more context about how you intend to mix the use of Boehm GC with original
new/delete?
The following program illustrates an abuse of operator new:
(Put on your helmet, safety belt, and prepare to crash!)
//----------------- "death_by_new.cc" ----------------------
#include <iostream>
#include <new>
#include <list>
using std::list;
using std::cerr;
using std::endl;
void*
operator new(size_t) throw() {
cerr << "BAH!" << endl;
return 0;
}
void
operator delete(void*) throw() {
cerr << "Humbug!" << endl;
}
int
main(int, char*[]) {
cerr << "new int:" << endl;
int* pi = new int;
cerr << "list<int>:" << endl;
list<int> foo;
// DIES HERE: BUS ERROR / SEG FAULT...
cerr << "new list node:" << endl;
foo.push_back(13);
delete pi;
return 0;
}
//------------------ end of death ----------------------
The constructor for list<int> will call some initializer for the
allocator with which it is linked, thus dying upon construction.
Final note: beware of global static initialization ordering if you supply
your own allocator. Allocations that occur before main() begins
may require special handling.
Happy hacking, hope this helped,
David Fang
Computer Systems Laboratory
Electrical & Computer Engineering
Cornell University
http://www.csl.cornell.edu/~fang/ -- the world's modem-friendliest web page
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden