[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    Re: kdelibs/khtml/rendering
From:       Luis Pedro Coelho <luis_pedro () netcabo ! pt>
Date:       2003-04-17 13:36:10
[Download RAW message or body]

Le Jeudi 17 Avril 2003 13:58, Michael Matz a écrit :
> On Wed, 16 Apr 2003, Zack Rusin wrote:
> > > -    void* operator new(size_t sz, RenderArena* renderArena);
> > > +    void* operator new(size_t sz, RenderArena* renderArena) throw();
> > 
> > "No throw guarantee" on operator new ? Are you sure about that one?
> 
> Of course it's possible to have nothrow() new's.  You would have to
> explicitely check the return values for being NULL, if you use those,
> _and_ want to be sure to not become hit by segfaults.

I think it is not as obvious as that. Are you confusing operator new with the 
new operator aka new expression (this is not a joke, "operator new" is a 
function which called by the "new" operator)?

I have been googling the c++ newsgroups (the moderated ones have a lot of 
quality) for an answer to this question and I only found the following thread 
which discusses a similiar question:

http://groups.google.com/groups?hl=pt-PT&lr=&ie=UTF-8&threadm=7kghg1%24n97%241%40engne \
ws1.eng.sun.com&rnum=4&prev=/groups%3Fq%3Doperator%2Bnew%2Bthrow()%2Bclass%2Bgroup:com \
p.std.c%252B%252B%26hl%3Dpt-PT%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.std.c%252B%252B%26selm%3D7kghg1%2524n97%25241%2540engnews1.eng.sun.com%26rnum%3D4


Which states that a replacement for ::operator new(size_t) may not return 0, 
but it implies a class specific one may do so.

I also didn't find any answer The C++ Language 3rd Ed. 

My version of g++ accepts that a class specific operator new(size_t) returns 0 
if and only if it is declared throw() [ read "info:/gcc/C++ Dialect Options" 
the -fcheck-new thingy ]. I mean if you try the following, you will get a 
warning and the constructor *will* be called on a null pointer, which is 
harmless here, but may lead to crashes in other situations:

----------
#include <iostream>
#include <new> // size_t definition

using namespace std;

void my_new_handler() {
         cout << "new handler called" << endl;
}

struct nomemory {

         nomemory() {
                    cout << "nomemory::nomemory()" << endl;
                      }
           void* operator new( size_t ) {
                      cout << "nomemory::operator new" << endl;
                         return 0; // "Out of memory"
                           }
};
int main() {
         set_new_handler( my_new_handler );
           nomemory* nm = new nomemory;
             cout << "nm = " << nm << endl;
             return 0;
}
---

All of this leads me to think that this is going into dark corners of the 
language that we should not touch them. Like Kernighan and Pike said "stay in 
the mainstream of the language." If you want a non throwing operator new call 
it operator new (size_t, std::nothrow_t) and if you just call it operator 
new, make it never return a null pointer, or your programm will crash.

regards,
-- 
Luis Pedro Coelho

check out my game of hearts for the KDE at

http://hearts.sf.net


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic