G
Guest
Hi all,
I've been using C++ for quite a while now and I've come to the point where I need to overload new
and delete inorder to track memory and probably some profiling stuff too. I know that discussions of
new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for
currently. I've searched around for about the last too weeks and have read up on new and overloading it to
some extent but there are still a few mysteries to me. First of all, how is the constructor called in relation
to a 'new' operator?
Foo* NewFoo = new Foo();
In most tutorials on overloading 'new', they usually put malloc in the body of the 'new' and, of course,
they put free() in the corresponding delete. But I thought that the reason you use new/delete was that
malloc doesn't call the constructor and free won't call the destructor.. but I don't see where or how
the constructors/destructors get called when you overload 'new' if you use malloc inside the overloaded
'new' and free in the overloaded 'delete'.
I have a Tracer class that I am trying to use to trace allocations and deallocations in my classes,
so currenlty all I really want to do is to have a new operator that is overloaded so that is passes in
both the filename and linenumber and then just use 'new' normally. Currently I'm not trying to using
any other allocater (though I'd like this solution to be flexible enough that I could) so all I really am
trying to do is something like this :
void * operator new (unsigned int size, char const * file, int line)
{
void * p = new (size);
if (Tracer::Ready)
NewTrace.Add (p, file, line, size);
return p;
}
I know this code won't work (I couldn't get it to compile) but consider it the pseudo code for what I'm
trying to accomplish.
I've also seen a few articles about using a class new operator. I only have a few base classes and I
only want to track the memory in my classes really, would it be possible to over load the new operator
for my base classes and have them work in my derived classes? Is there an accepted or
preferred way of overloading new and delete so that only extra functionality is added but the
actual new and delete are still used, like in the above pseudocode where the call to overloaded new actually
does call new it just does a few extra things?
Also on a different topic, is there a defined way that objects are destructed when they go out of scope?
In this case my Tracer in instantiated right above my main(), but I also have a static vector of objects
in a factory class. It looks like my non-static Tracer goes out of scope and is therefore destroyed
before the static vector is destructed. That does make some sense but I wanted to know for sure if this
is what is being done or does it only destruct in that order because of the way the compiler works,i.e. its
undefined in the standard?
Any bizzareness of this post is a direct lack of sleep
Any help would be much appreciated.
I've been using C++ for quite a while now and I've come to the point where I need to overload new
and delete inorder to track memory and probably some profiling stuff too. I know that discussions of
new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for
currently. I've searched around for about the last too weeks and have read up on new and overloading it to
some extent but there are still a few mysteries to me. First of all, how is the constructor called in relation
to a 'new' operator?
Foo* NewFoo = new Foo();
In most tutorials on overloading 'new', they usually put malloc in the body of the 'new' and, of course,
they put free() in the corresponding delete. But I thought that the reason you use new/delete was that
malloc doesn't call the constructor and free won't call the destructor.. but I don't see where or how
the constructors/destructors get called when you overload 'new' if you use malloc inside the overloaded
'new' and free in the overloaded 'delete'.
I have a Tracer class that I am trying to use to trace allocations and deallocations in my classes,
so currenlty all I really want to do is to have a new operator that is overloaded so that is passes in
both the filename and linenumber and then just use 'new' normally. Currently I'm not trying to using
any other allocater (though I'd like this solution to be flexible enough that I could) so all I really am
trying to do is something like this :
void * operator new (unsigned int size, char const * file, int line)
{
void * p = new (size);
if (Tracer::Ready)
NewTrace.Add (p, file, line, size);
return p;
}
I know this code won't work (I couldn't get it to compile) but consider it the pseudo code for what I'm
trying to accomplish.
I've also seen a few articles about using a class new operator. I only have a few base classes and I
only want to track the memory in my classes really, would it be possible to over load the new operator
for my base classes and have them work in my derived classes? Is there an accepted or
preferred way of overloading new and delete so that only extra functionality is added but the
actual new and delete are still used, like in the above pseudocode where the call to overloaded new actually
does call new it just does a few extra things?
Also on a different topic, is there a defined way that objects are destructed when they go out of scope?
In this case my Tracer in instantiated right above my main(), but I also have a static vector of objects
in a factory class. It looks like my non-static Tracer goes out of scope and is therefore destroyed
before the static vector is destructed. That does make some sense but I wanted to know for sure if this
is what is being done or does it only destruct in that order because of the way the compiler works,i.e. its
undefined in the standard?
Any bizzareness of this post is a direct lack of sleep
Any help would be much appreciated.