Exceptions handling in pure C

J

James Kuyper

I compile with http://llvm.org/demo/index.cgi
and it call:
__cxa_allocate_exception
__cxa_throw
__cxa_begin_catch
__cxa_end_catch
how it call in own program?

I've found documentation for those functions at
<http://libcxxabi.llvm.org/spec.html>, though I didn't dig into the
documentation in any depth. However, given the way that they're nbamed
and the fact that they're identified as being part of libc++, I would
make two guesses:

1) you might not need to call those functions explicitly when compiling
for C++, the calls might be generated automatically by the compiler in
connection with throw() and catch() constructs.

2) it might not be safe to call them from C.

However, those are just guesses - only someone actually familiar with
llvm could address those issue authoritatively. So ask your question in
a forum devoted to llvm.
 
M

Malcolm McLean

How can translate C++ to C code with exceptions handling? Unfortunately
Stroustrup translator cfron has only source and not compiled, llvm command
to translate C++ => C not works.
You mean hand translation?

Translate a throw to a goto error_exit.
In the error_exit portion of the code, free up everything you allocated
in the function (so be sure not to have dangling uninitialised pointers).#
Then set a global "exception" flag.
After every call to a function which can throw, add If(expeption) goto error_exit;

To implement catch, check the exeception flag, then clear it. You'll have to
have jumps into catch.

It's not nice. But it will work without too much violence to your C code.

You have to be more sophisticated with automatic translation, because you
have to handle every legal C++ construct.
 
J

Jorgen Grahn

Is problem - I need both Windows and Linux compiler, comeau sold only one,
two compilers will be too expensive

Why do you want pure C, anyway? If what you have now is C++, and you
want a typical C++ feature like exceptions, why not continue with C++?

Some people avoid C++ because the language is more complex than C, but
C code created by automatic translation from C++ (not to mention the
exception thing) will be *far worse* than either C or C++.

/Jorgen
 
J

Jorgen Grahn

Is problem - I need both Windows and Linux compiler, comeau sold only one,
two compilers will be too expensive

Why do you want pure C, anyway? [...]

Never mind; it was explained (sort of) elsewhere in the thread.

/Jorgen
 
B

BGB

Cfront (Stroustrup's original C++ to C translator) was abandoned when
it became obvious that adding exceptions (which were a fairly late
addition to C++), was near impossible. So Cfront never supported
exceptions.

and, apparently no one considered doing it like something like this:

if(ehBeginTry())
{
... do stuff ...
ehEndTry();
}else if(ehCatch("SomeException"))
{
... handle stuff ...
ehEndCatch();
}else
....
}else if(ehBeginFinally())
{
... cleanup ...
ehEndFinally(); //continues unwinding
}


where "ehBeginTry()" would register the exception-handler, and
"ehEndTry()" would unregister it.

the "ehBeginTry()" return value will indicate whether they are entering
the "try" block, or handling a thrown exception.


in cases where resources need to be cleaned up, a block can be written like:
if(ehBeginTry())
{
... do stuff ...
ehEndTry();
}else if(ehBeginFinally())
{
... cleanup ...
ehEndFinally();
}



or such...
 
T

Thomas Richter

I compile with http://llvm.org/demo/index.cgi
and it call:
__cxa_allocate_exception
__cxa_throw
__cxa_begin_catch
__cxa_end_catch
how it call in own program?

You don't. You write C++ in first place. Look, the way *how* a C++
compiler implements exceptions is defined by the specific compiler
implementation. G++ for example does not generate much code, instead it
creates a table (within the text segment, i.e. same place where the code
goes) which defines which objects to clean up. This table is then parsed
by the exception handling mechanism.

What you try to do is not feasible - you can "simulate" *some* of the
features of exceptions in C, but not all. The troublesome part is that
you need to call the destructors of all automatic objects when
propagating an exception out of a function.

If your C++ code is written in such a way that all automatic objects
have trivial destructors, one can somehow emulate exception handling by
setjmp/longjmp (been there, done that), but given that you have already
an existing C++ code - no way.

There are free C++ compilers out there, for example the mentioned GNU
g++. Just use it - problem solved.

Greetings,
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,567
Members
47,203
Latest member
EmmaSwank1

Latest Threads

Top