Unix Programming - C/C++ exceptions

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > C/C++ exceptions





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author C/C++ exceptions
Russell Shaw

2005-05-05, 6:03 pm

Hi,
I'm using setjmp/longjmp to do C++-like exceptions in C. As long as i catch all
longjmps within a C function, is it ok to link the code into a C++ program
without conflicts with the C++ exceptions and other things?
Martijn Lievaart

2005-05-05, 6:03 pm

On Fri, 06 May 2005 03:12:46 +1000, Russell Shaw wrote:

> Hi,
> I'm using setjmp/longjmp to do C++-like exceptions in C. As long as i
> catch all longjmps within a C function, is it ok to link the code into a
> C++ program without conflicts with the C++ exceptions and other things?


Exceptions, even when propagating trough C code, will work, so no problem
there. Longjumps that do not propagate through C++ code are OK too.
Longjumps propagating through C++ code? Officially not, but probably
works. Ask in a group for your compiler.

HTH,
M4

--
Ah, the beauty of OSS. Hundreds of volunteers worldwide volunteering
their time inventing and implementing new, exciting ways for software
to suck. -- Toni Lassila in the Monastry

Pascal Bourguignon

2005-05-05, 6:03 pm

Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:

> Hi,
> I'm using setjmp/longjmp to do C++-like exceptions in C. As long as i catch all
> longjmps within a C function, is it ok to link the code into a C++ program
> without conflicts with the C++ exceptions and other things?


Well, the problem that may occur is that finalizers probably won't be
called when you longjmp. longjmp is an orthogonal mechanism. C++
would have to know that you're leaving the building, but longjmp won't
tell.

--
__Pascal Bourguignon__ http://www.informatimago.com/

This is a signature virus. Add me to your signature and help me to live
Russell Shaw

2005-05-06, 2:52 am

Pascal Bourguignon wrote:
> Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
>
>
> Well, the problem that may occur is that finalizers probably won't be
> called when you longjmp. longjmp is an orthogonal mechanism. C++
> would have to know that you're leaving the building, but longjmp won't
> tell.


Hi,
I was restricting the longjmps to be caught within a function that has no
C++ classes, so hopefully there's no C++ destructors there. (I assume normal
structs have no destructors in C++?)
Russell Shaw

2005-05-06, 2:52 am

Russell Shaw wrote:
> Pascal Bourguignon wrote:
>
>
> Hi,
> I was restricting the longjmps to be caught within a function that has no
> C++ classes, so hopefully there's no C++ destructors there. (I assume
> normal structs have no destructors in C++?)


Found one answer anyway: http://cpptips.hyperformix.com/cpptips/setjmp
Martijn Lievaart

2005-05-06, 7:48 am

On Fri, 06 May 2005 12:57:53 +1000, Russell Shaw wrote:

> Hi,
> I was restricting the longjmps to be caught within a function that has no
> C++ classes, so hopefully there's no C++ destructors there. (I assume normal
> structs have no destructors in C++?)


Structs and classes are the same, the only difference is the default
access specifier. But other that that, if you use a simple struct there is
guarenteed no destructor. The standard spells out when this happens, but
if you use a C compatible struct it is guarenteed not to have a
destructor. It should be compatible with C after all :-)

You also should not use throw specifiers. Ask in a C++ group why if your
interested.

M4
--
Ah, the beauty of OSS. Hundreds of volunteers worldwide volunteering
their time inventing and implementing new, exciting ways for software
to suck. -- Toni Lassila in the Monastry

Bjorn Reese

2005-05-06, 6:02 pm

Martijn Lievaart wrote:

> Longjumps propagating through C++ code? Officially not, but probably
> works. Ask in a group for your compiler.


This is dangerous business. Longjumps simply reset the stack pointer;
they do not perform stack unwinding as exceptions do. So no destructors
of local objects are called when longjumping. This means that resources
may be leaked and smart pointers will start misbehaving.

Resource leaks is also a problem that should be considered when
longjumping in C code. With return codes you get the change to clean
up local resources, with longjump they will be leaked.

If I want something exception-like in C, I tend to avoid longjump and
instead use a combination of goto statements within a function and
return code across functions.

--
mail1dotstofanetdotdk
Martijn Lievaart

2005-05-11, 5:56 pm

On Fri, 06 May 2005 16:11:40 +0200, Bjorn Reese wrote:

> Martijn Lievaart wrote:
>
>
> This is dangerous business. Longjumps simply reset the stack pointer;
> they do not perform stack unwinding as exceptions do. So no destructors
> of local objects are called when longjumping. This means that resources
> may be leaked and smart pointers will start misbehaving.


You are absolutely right. What I wanted to say is that officially it is
not supported, but in limited circumstances (basically C++ used as a btter
C) you can get away with it.

> Resource leaks is also a problem that should be considered when
> longjumping in C code. With return codes you get the change to clean up
> local resources, with longjump they will be leaked.


That is a problem of longjumps in general.

<rant>
A bit OT, but whatever. I recently went to an OS conference (FOSDEM) and
one of the speakers asked who ever saw a C++ program not leaking memory. I
was the only one raising my finger. C++ is a XXXXX to learn, but once you
learn C++ proper, you /never/ have to worry about memory leaks anymore.
Mind you, might take some years to get there. However, my C++ programs
never leak. And many of them run 24/7. Could not have done that in C.

Recommended reading: Sutter: Exceptionel C++; Meyers: Effective STL. Once
you read those, you know how to program in C++ and why you want another
language. Except it does not exist. Yet.
</rant>

M4
--
Ah, the beauty of OSS. Hundreds of volunteers worldwide volunteering
their time inventing and implementing new, exciting ways for software
to suck. -- Toni Lassila in the Monastry

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com