Overloading operators new and delete

  • Follow


Dear experts,

Please, consider the following example code:

void *
CLASS_A::operator new( size_t size ){
   return Stack<sizeof(CLASS)>::pop();
}

void
CLASS_A::operator delete( void *p ){
   Stack<sizeof(CLASS)>::push( p );
}

The objective of this code is to reuse the memory allocated whenever I
create a new object of type CLASS_A.
Unfortunatelly, the code is not working as expected, as the memory is
still being freed after a call to delete. Is the memory suppose to be
free after delete, even though I'm not freeing it in the operator's
implementation?

Thanks,

William.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply williamvoor 12/12/2004 10:10:17 AM

William Voorsluys wrote:
> Dear experts,
>
> Please, consider the following example code:
>
> void *
> CLASS_A::operator new( size_t size ){
>    return Stack<sizeof(CLASS)>::pop();
> }
>
> void
> CLASS_A::operator delete( void *p ){
>    Stack<sizeof(CLASS)>::push( p );
> }
>
> The objective of this code is to reuse the memory allocated
> whenever I create a new object of type CLASS_A.
> Unfortunatelly, the code is not working as expected, as the
> memory is still being freed after a call to delete. Is the
> memory suppose to be free after delete, even though I'm not
> freeing it in the operator's implementation?

Your concept should work. You just overrode operator delete
incorrectly. It takes a size_t as well as a void *. For example, I have
this in some code of mine, to memset garbage over a block of memory
before it is deleted.

void *CH::operator new(size_t size)
{
return new char[size];
}

void CH::operator delete(void *mem, size_t size)
{
delete[] static_cast<char *>( memset(mem, 0xdeaddead, size) );
}

.... bleh, Google Beta seems to eat my indentation. I can't figure out
how to fix it...

-- 
Dave O'Hearn


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Dave 12/12/2004 11:08:38 PM


Dave O'Hearn wrote:
> William Voorsluys wrote:

> > Please, consider the following example code:

> > void *
> > CLASS_A::operator new( size_t size ){
> >    return Stack<sizeof(CLASS)>::pop();
> > }

> > void
> > CLASS_A::operator delete( void *p ){
> >    Stack<sizeof(CLASS)>::push( p );
> > }

> > The objective of this code is to reuse the memory allocated
whenever
> > I create a new object of type CLASS_A. Unfortunatelly, the code is
> > not working as expected, as the memory is still being freed after a
> > call to delete. Is the memory suppose to be free after delete, even
> > though I'm not freeing it in the operator's implementation?

> Your concept should work. You just overrode operator delete
> incorrectly. It takes a size_t as well as a void *.

Both are legal.  I've often done this without the size_t.

The only thing I can think of is that either the code which constructs
and deletes the objects does not see the declaration of these operators
in the class definition, or that he is allocating and deleting arrays
of
CLASS_A, in which case, the operators which will be used will be
operator new[] and operator delete[].

> For example, I have this in some code of mine, to memset garbage over
> a block of memory before it is deleted.

I do that normally in a global operator delete (along with a few other
verifications set up in the operator new).

> void *CH::operator new(size_t size)
> {
> return new char[size];
> }

> void CH::operator delete(void *mem, size_t size)
> {
> delete[] static_cast<char *>( memset(mem, 0xdeaddead, size) );

You realize, of course, that this sets all of the bytes in the memory
to
0xAD.  Memset only uses the low order byte of its second parameter.

> }

> ... bleh, Google Beta seems to eat my indentation. I can't figure out
> how to fix it...

Don't feal bad; I've got the same problem.

--
James Kanze           GABI Software         http://www.gabi-soft.fr
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply kanze 12/14/2004 3:35:59 AM

> Your concept should work. You just overrode operator delete
> incorrectly. It takes a size_t as well as a void *. 

Actually, it can be either.

Also, while we're on the subject of memory allocators, you might want to 
check out my article on them (mostly referring to C, but includes lots 
of C++ allocator links):

http://www-106.ibm.com/developerworks/linux/library/l-memory/

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Jonathan 12/14/2004 8:27:30 PM

3 Replies
403 Views

(page loaded in 0.465 seconds)

Similiar Articles:













7/15/2012 5:28:28 PM


Reply: