473,480 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

exit() from within C++ program

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

May 10 '07 #1
7 6168
chris wrote:
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!

--
Ian Collins.
May 10 '07 #2

"chris" <cr******@indiana.eduwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
>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.

>

May 10 '07 #3
GeekBoy wrote:
"chris" <cr******@indiana.eduwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
>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.

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

--
Ian Collins.
May 10 '07 #4
On May 10, 3:26 pm, chris <craph...@indiana.eduwrote:
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; }
}

May 10 '07 #5
On 10 Maj, 07:01, "GeekBoy" <geeek...@yeehaw.comwrote:
"chris" <craph...@indiana.eduwrote in message

news:11**********************@y80g2000hsf.googlegr oups.com...
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.
What part of that comes from the iostream library?
And return 0 only works in main, other functions might have other
return-types.

--
Erik Wikström

May 10 '07 #6
On 9 May 2007 20:26:17 -0700 in comp.lang.c++, chris
<cr******@indiana.eduwrote,
>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.

May 10 '07 #7
Old Wolf wrote:
>
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
May 10 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
533
by: Siemel Naran | last post by:
(1) About std::exit. If I call this function, will the system call the destructors of all objects in the call stack. Eg. void f() { std::exit(1); } void g() { X x; f(); } Does the call in...
11
29634
by: Peter Steele | last post by:
I have a Windows application written in C# that I want to return a non-zero exit code when it it run. The problem is that as a Windows application, there doesn't seem to be a way to control this....
20
3558
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
16
1625
by: jayapal | last post by:
hi all, what is the differrence b/w the usage or return and the exit in the C programming.. thanks, jay
2
6392
by: jaddle | last post by:
I program I'm writing crashes when it exits with a segfault. I'm runnning under linux. Using gdb, i see that the crash happens after the last line of main(), which is just a return(EXIT_SUCCESS); If...
4
6165
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages...
11
2246
by: =?Utf-8?B?U3RldmVEQjE=?= | last post by:
Hi all. I'm using VS 2008 Express C++. I created a console application back in 1999, and updated it for VC++ 6.0 in 2001. I've updated again this past month, and have found enough differences...
5
1890
by: Andrew Greensted | last post by:
Hi All, I've written a simple c program that reads stdin, adds highlighting to particular words and outputs the result to stdout. I invoke it as follows (where xil-highlight is my program): ...
39
2766
by: mathieu | last post by:
Hi there, I am trying to reuse a piece of code that was designed as an application. The code is covered with 'exit' calls. I would like to reuse it as a library. For that I renamed the 'main'...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5357
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.