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

List:       kde-devel
Subject:    Re: Exceptions, libraries, gcc and new
From:       Dominique Devriese <dominique.devriese () student ! kuleuven ! ac ! be>
Date:       2002-10-28 21:38:48
[Download RAW message or body]

Bo Thorsen writes:

>> What I don't understand, though, is how gcc and the stdlib behave
>> when exeptions are enabled. Specifically, what does operator new do
>> in case of failure? Does it throw an exception or does it return 0?
>>
>> What's the right way to deal with this?

> operator new in a standard complying c++ compiler throws the
> bad_alloc exception. You can not use the return value for anything,
> so this won't work (well, it probably can for some compilers but you
> can never be sure):

> try {
>   a = new A();
> } catch (...) { } if (a == 0)
>   // handle problems here...

Are you sure ?
if i understand you correctly, then technically, you are not talking
about "operator new", but about the "new" operator ( yes, there _is_ a
difference ).  the new operator can't be overridden.  What it does is:
1 it calls operator new to allocate memory
2 it calls the destructor on the allocated memory.

"operator new" can be overridden.  What it does is simply allocate
memory.  I think i remember that g++ generated one that returns 0
instead of throwing bad_alloc if you compile without exception
support, but i could be off there..
Same goes for "operator new[]" which does the same as the previous but
for arrays...

What I think can be a problem is that the code generated for "new
A()" will expect "operator new" to return 0 on error, where it will
not, but instead throw an exception, since it is generated with the
main() support code, and that one is compiled with exceptions
support..

This way, it will bypass the code that called it, and you will get an
exception that prolly won't be caught, and terminate() the program..

But again: i could be off :)

> You must handle it in the catch block. If you want to compile this
> both with and without exceptions, the best trick is to set a to 0 in
> the catch block.

hm.. does gcc compile code that has "try" and "catch"'es when
exception support is disabled ?  And of course the problem seems to be
not to do this yourself, but to somehow work around Qt and KDE not
doing this ( which seems impossible to me.. )

cheers
domi
-- 
Beware of a tall blond man with one black shoe.
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

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