exit() from within C++ program

C

chris

I am writing a C++ program for which the guts of the program is about
100,000 lines of straight c code.
This c code is peppered with hundreds of exit() statements which,
unfortunately, cause a pretty ungraceful
termination of my program. Someone likened the exit() to be like
pulling the plug on an appliance when used within C++. Without
changing each instance of exit to something else, I would like to
change
things so that my program will at least terminate normally, maybe with
a concilliatory message. I am
basically a c programmer, not really at home in the C++ world. Any
advice would be appreciated.

Chris
 
I

Ian Collins

chris said:
I am writing a C++ program for which the guts of the program is about
100,000 lines of straight c code.
This c code is peppered with hundreds of exit() statements which,
unfortunately, cause a pretty ungraceful
termination of my program. Someone likened the exit() to be like
pulling the plug on an appliance when used within C++. Without
changing each instance of exit to something else, I would like to
change
things so that my program will at least terminate normally, maybe with
a concilliatory message. I am
basically a c programmer, not really at home in the C++ world. Any
advice would be appreciated.
If your application isn't intended to be portable, you probably want a
platform specific way to replace the library function exit() with your
own version, try a platform specific group for a solution.

Good luck!
 
G

GeekBoy

chris said:
I am writing a C++ program for which the guts of the program is about
100,000 lines of straight c code.
This c code is peppered with hundreds of exit() statements which,
unfortunately, cause a pretty ungraceful
termination of my program. Someone likened the exit() to be like
pulling the plug on an appliance when used within C++. Without
changing each instance of exit to something else, I would like to
change
things so that my program will at least terminate normally, maybe with
a concilliatory message. I am
basically a c programmer, not really at home in the C++ world. Any
advice would be appreciated.

Chris



Try ' return 0; ' (number zero) from the iostream library.
 
O

Old Wolf

Without changing each instance of exit to something else, I would like to
change things so that my program will at least terminate normally,
maybe with a concilliatory message.

You could do that by registering a handler with atexit(). In fact
this is possible in C as well.

Of course a better solution is to design your C API so that
it returns error codes instead of calling exit().

NB. Here is a brutal hack I thought of that might work.
In a common header file for your C code (that is NOT
included by any C++ code), write:
#define exit(arg) hack_exit(arg)
void hack_exit(int x);

Then in one of the C++ modules write:
extern "C" void hack_exit(int x) {
throw std::runtime_error("C code tried to exit");
}

Then any attempted exit by the C code would show up as an
exception that can be handled by the C++ part. Which could
even just be wrapping main:

int main2() { /* the main program */ }
int main() {
try { main2(); }
catch(std::execption &e) { std::cout << "Aborting: " << e.what() <<
std::endl; }
}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Try ' return 0; ' (number zero) from the iostream library.

What part of that comes from the iostream library?
And return 0 only works in main, other functions might have other
return-types.
 
D

David Harmon

On 9 May 2007 20:26:17 -0700 in comp.lang.c++, chris
I am writing a C++ program for which the guts of the program is about
100,000 lines of straight c code.
This c code is peppered with hundreds of exit() statements

Time to warm up the old global replace edit tool.
Change all those exit() calls to something you control.
 
L

Lars Tetzlaff

Old said:
Then in one of the C++ modules write:
extern "C" void hack_exit(int x) {
throw std::runtime_error("C code tried to exit");
}

Then any attempted exit by the C code would show up as an
exception that can be handled by the C++ part. Which could
even just be wrapping main:

But C doesn't know exceptions. This may work in some implementations
(VC++, gnu with the appropriate option when compiling the C code) but it
isn't portable.

Lars
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top