Topic: global new/delete overload for garbage collection
Author: e0226430@student.tuwien.ac.at (Sebastian Redl)
Date: Mon, 4 Dec 2006 15:43:29 GMT Raw View
James Kanze wrote:
> bool initialized = false ;
>
> void*
> operator new( size_t n ) throw ( std::bad_alloc )
> {
> static bool recursing = false ;
> if ( initialized && ! recursing ) {
> recursing = true ;
> std::cerr << "new invoked" << std::endl ;
> recursing = false ;
> }
> void* p = malloc( n ) ;
> if ( p == NULL ) {
> throw std::bad_alloc() ;
> }
> return p ;
> }
This code still lacks the logic for new_handler, though.
while(true) {
void *p = malloc(n);
if(p) {
return p;
} else {
new_handler *tmp = set_new_handler(0);
set_new_handler(tmp);
if(tmp) {
tmp();
} else {
throw std::bad_alloc();
}
}
}
--
Sebastian Redl
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "jam" <farid.mehrabi@gmail.com>
Date: Tue, 28 Nov 2006 09:41:48 CST Raw View
eca wrote:
> ===================================== MODERATOR'S COMMENT:
>
> I'm approving this, but we should focus on what the standard says instead of
> on details of one specific compiler.
>
> ------=_Part_58573_14344447.1164402546252
> Content-Type: text/html; charset=ISO-8859-1
> Content-Transfer-Encoding: 7bit
> Content-Disposition: inline
>
> approve<br>comment<br>I'm approving this, but we should focus on what the standard says instead of on details of one specific compiler.<br>
>
> ------=_Part_58573_14344447.1164402546252--
>
>
> ===================================== END OF MODERATOR'S COMMENT
>
> jam wrote:
> > the following works on vc.net 7.0 is it portable?
> >
> > void * operator new(size_t sz){
> > cout<<"new invoked"<<endl;
> > return malloc(sz);
> > };
> >
> > void operator delete(void *ptr){
> > cout<<"delete invoked"<<endl;
> > free(ptr);
> > };
> >
>
> If your purpose is garbage collection, I would be extremely careful in
> case your project includes DLLs. AFAIK, global new and delete
> redefinition is limited to the "project" (in the VC sense) they are
> defined in, and is not application-wide.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
thx evry 1.
Its silly to output something to usr at mem alloc/dealloc time in a
real project ; it is just a testing program.actually I tested some
similar code via visual output that worked well but when writting this
topic I carelessly changed the output to cout.anyway I think that if
the output tool does not have much inline dependencies the code should
run correctly.what I was asking was about the syntax.and u all helped a
lot.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "James Kanze" <james.kanze@gmail.com>
Date: Fri, 24 Nov 2006 12:38:34 CST Raw View
jam wrote:
> the following works on vc.net 7.0 is it portable?
> void * operator new(size_t sz){
> cout<<"new invoked"<<endl;
> return malloc(sz);
> };
> void operator delete(void *ptr){
> cout<<"delete invoked"<<endl;
> free(ptr);
> };
No. It shouldn't even compile, if you've included <new> (or is
some other header you've included includes <new>), since the
standard versions of the functions use exception specifiers.
And of course, since operator new isn't allowed to return a null
pointer (which malloc does), you need to test this, and throw
bad_alloc. And to use bad_alloc, you need to include <new> (and
your code no longer compiles).
Also, it's highly likely that outputting to a stream uses
operator new, resulting in endless recursion, and it's highly
likely that some static initializers will invoke operator new
before cout has been constructed. I generally use something
like the following for quick tests:
bool initialized = false ;
void*
operator new( size_t n ) throw ( std::bad_alloc )
{
static bool recursing = false ;
if ( initialized && ! recursing ) {
recursing = true ;
std::cerr << "new invoked" << std::endl ;
recursing = false ;
}
void* p = malloc( n ) ;
if ( p == NULL ) {
throw std::bad_alloc() ;
}
return p ;
}
void
operator delete( void* p ) throw ()
{
// I probably should use a recursing flag here as
// well, but delete has never caused me any problems.
std::cerr << "delete invoked" << std::endl ;
free( p ) ;
}
I then set initialized in main.
A more general solution would be to drop the initialized flag,
and use something like:
if ( ! recursing ) {
recursing = true ;
static std::ios::Init dummyForInitialization ;
std::cerr << "new invoked" << std::endl ;
recursing = false ;
}
in the if.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
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
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "eca" <enrico.ecamail@gmail.com>
Date: Fri, 24 Nov 2006 15:09:25 CST Raw View
===================================== MODERATOR'S COMMENT:
I'm approving this, but we should focus on what the standard says instead of
on details of one specific compiler.
------=_Part_58573_14344447.1164402546252
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
approve<br>comment<br>I'm approving this, but we should focus on what the standard says instead of on details of one specific compiler.<br>
------=_Part_58573_14344447.1164402546252--
===================================== END OF MODERATOR'S COMMENT
jam wrote:
> the following works on vc.net 7.0 is it portable?
>
> void * operator new(size_t sz){
> cout<<"new invoked"<<endl;
> return malloc(sz);
> };
>
> void operator delete(void *ptr){
> cout<<"delete invoked"<<endl;
> free(ptr);
> };
>
If your purpose is garbage collection, I would be extremely careful in
case your project includes DLLs. AFAIK, global new and delete
redefinition is limited to the "project" (in the VC sense) they are
defined in, and is not application-wide.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: pete@versatilecoding.com (Pete Becker)
Date: Wed, 22 Nov 2006 18:41:57 GMT Raw View
jam wrote:
> the following works on vc.net 7.0 is it portable?
>
> void * operator new(size_t sz){
> cout<<"new invoked"<<endl;
> return malloc(sz);
> };
>
> void operator delete(void *ptr){
> cout<<"delete invoked"<<endl;
> free(ptr);
> };
>
Yes in the sense that you mean, but not really. <g>
Yes, you can supply your own operator new and operator delete, and the
syntax you've used is correct. Not really, because inserting into a
stream can cause a memory allocation, which will then use your operator
new again, which will again attempt to insert into a stream, and so on,
ad infinitum. Use printf to instrument operator new and operator delete.
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "James Kanze" <james.kanze@gmail.com>
Date: Fri, 24 Nov 2006 12:39:02 CST Raw View
Pete Becker wrote:
> jam wrote:
> > the following works on vc.net 7.0 is it portable?
> > void * operator new(size_t sz){
> > cout<<"new invoked"<<endl;
> > return malloc(sz);
> > };
> > void operator delete(void *ptr){
> > cout<<"delete invoked"<<endl;
> > free(ptr);
> > };
> Yes in the sense that you mean, but not really. <g>
Actually, I doubt that it works reliably in VC ++ either. I've
never seen an implementation of iostream which didn't call
operator new sometimes.
> Yes, you can supply your own operator new and operator delete, and the
> syntax you've used is correct.
Not really. He needs exception specifiers. Also, operator new
may not return NULL, malloc can, so he needs to cover that
eventuality.
> Not really, because inserting into a
> stream can cause a memory allocation, which will then use your operator
> new again, which will again attempt to insert into a stream, and so on,
> ad infinitum. Use printf to instrument operator new and operator delete.
Printf can also call operator new (although it's much less
likely, since most implementations will share printf with the
implementation of C, where there isn't an operator new).
There's also no guarantee that cout will be constructed before
the first call to operator new---I actually encountered this
problem with g++ under Solaris.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
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
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "jam" <farid.mehrabi@gmail.com>
Date: Wed, 22 Nov 2006 10:36:44 CST Raw View
the following works on vc.net 7.0 is it portable?
void * operator new(size_t sz){
cout<<"new invoked"<<endl;
return malloc(sz);
};
void operator delete(void *ptr){
cout<<"delete invoked"<<endl;
free(ptr);
};
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]