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: Overloading operators new and delete - comp.lang.c++.moderated ...Dear experts, Please, consider the following example code: void * CLASS_A::operator new( size_t size ){ return Stack::pop(); } void CLASS_A::ope... Overloading dereference pointers to pointers to class members ...Overloading operators new and delete - comp.lang.c++.moderated ... Overloading dereference pointers to pointers to class members ... 12.7 ... SMART POINTERS: OVERLOADING ... STL allocators, global new/delete using the heap and shared memory ...Overloading operators new and delete - comp.lang.c++.moderated ... STL allocators, global new/delete using the heap and shared memory ... The STL containers use allocator ... between-and operator - comp.soft-sys.sasOverloading operators new and delete - comp.lang.c++.moderated ... Dear experts, Please, consider the following example code: void * CLASS_A::operator new( size_t size ... Non-member operator overloading, linker complains - comp.lang.c++ ...Non-member operator overloading, linker complains - comp.lang.c++ ... Problem is, that U ... pragmas or linker ... allocate memory from Windows, they call the operator new ... tdelete() example? - comp.lang.cOverloading operators new and delete - comp.lang.c++.moderated ... Dear experts, Please, consider the following example code: void * CLASS_A::operator new( size_t size ... sizeof implementation - comp.lang.cOverloading operators new and delete - comp.lang.c++.moderated ..... code: void * CLASS_A::operator new( size_t size ){ return Stack<sizeof(CLASS ... to be free after ... Set implementation in STL - comp.lang.c++.moderatedOverloading operators new and delete - comp.lang.c++.moderated ..... even though I'm not freeing it in the operator's implementation? ... delete (along with a few other ... enum and operator++ - comp.lang.c++Write your own "new" operator - comp.lang.c++.moderated ... b) Type cast the ... operator « Operator Overloading « C++ Tutorial enum operator « Operator Overloading ... comp.lang.c++How to use parameter in operator delete? 2 1 (7/6/2012 1:42:45 AM) I implement operator new and operator delete by adding an extra paramet= er. If I want to implement one ... C++ in Action Book: TechiquesC++ In Action: Techniques Overloading operator new. Both new and delete are considered operators in C++. What it means, in particular, is that they can be overloaded ... Operator New and Operator Delete in C++ - Cprogramming.comCustomized Allocators with Operator New and Operator Delete by Andrei Milea Why Customize Memory Allocation by Overloading New and Delete? At times, you will have ... 7/15/2012 5:28:28 PM
|