C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

use of exit(-1)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
AccesDenied
Guest





PostPosted: Wed Oct 19, 2005 2:00 am    Post subject: use of exit(-1) Reply with quote



Can anyone tell me what is use of exit(-1) ..specificaly with with
EXIT_STATUS -1


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jack Klein
Guest





PostPosted: Wed Oct 19, 2005 8:43 am    Post subject: Re: use of exit(-1) Reply with quote



On 18 Oct 2005 22:00:21 -0400, "AccesDenied" <amit.kamal (AT) gmail (DOT) com>
wrote in comp.lang.c++.moderated:

Quote:
Can anyone tell me what is use of exit(-1) ..specificaly with with
EXIT_STATUS -1

The C language standard, which the C++ standard "inherits" in proper
object oriented fashion, only defines three portable values for
program termination, either by returning from main() or as an argument
to exit():

1. The macro EXIT_SUCCESS defined in <stdlib.h>/<cstdlib>.

2. The macro EXIT_FAILURE defined in <stdlib.h>/<cstdlib>.

3. The value 0.

The result of using 0 is exactly the same as using EXIT_SUCCESS.

The C++ standard, in section 18.3, specifically states:

"Finally, control is returned to the host environment. If status is
zero or EXIT_SUCCESS, an implementation-defined form of the status
successful termination is returned. If status is EXIT_FAILURE, an
implementation-defined form of the status unsuccessful termination is
returned. Otherwise the status returned is implementation-defined."

So if you use any other value as a return from main() or an argument
to exit(), it means whatever your compiler and operating system decide
that it means.

Note on many platforms such as MS-DOS and *NIX, the int value is
truncated to an 8-bit unsigned character so using -1 results in these
systems seeing a value of 255.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Maeder
Guest





PostPosted: Wed Oct 19, 2005 8:46 am    Post subject: Re: use of exit(-1) Reply with quote



"AccesDenied" <amit.kamal (AT) gmail (DOT) com> writes:

Quote:
Can anyone tell me what is use of exit(-1) ..specificaly with with
EXIT_STATUS -1

The C and C++ Standards define the symbols EXIT_SUCCESS and
EXIT_FAILURE for signaling success and failure, respectively, to the
outer world. Whether one of them is equal to -1 is unspecified.

So the outcome of using the exit code -1 is application or platform
defined.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Wed Oct 19, 2005 8:47 am    Post subject: Re: use of exit(-1) Reply with quote

In article <1129625968.331879.248280 (AT) g44g2000cwa (DOT) googlegroups.com>,
AccesDenied <amit.kamal (AT) gmail (DOT) com> wrote:

Quote:
Can anyone tell me what is use of exit(-1) ..specificaly with with
EXIT_STATUS -1

void std::exit(int) terminates the program WITHOUT destructing

objects of automatic duration. and returning to the system the value of
its argument, [similiar to the return value of main if specified] Beware
exit does not destruct anything on the stack in the calling function or
any of its parent. [6.6 para 2 of jan 2005 draft]. Should be close in
1998 adopted standard.

I don't recommend exit() in new code, a try block in main
catching a specific exception containing the int that would
be passed to exit() [throwing it instead of calling the function
exit(). ] then the cach block could just return the contained
integer from main and all gets destructed properly.

something like this is better C++ code to emulate exit(n)
unless you really do want non destructed objects on quitting.

struct exit_now
{
int ret_val;
exit_now(int a):ret_now(a){}
};

int main()
{
try
{
//....
} catch(exit_now &x)
{
return x.ret_val;
}
return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Thu Oct 20, 2005 11:20 am    Post subject: Re: use of exit(-1) Reply with quote

Jack Klein wrote:

[...]
Quote:
Note on many platforms such as MS-DOS and *NIX, the int value
is truncated to an 8-bit unsigned character so using -1
results in these systems seeing a value of 255.

Note too that on Unix and Unix-like, return codes greater than
128 are normally used to indicate that the program was
terminated by a signal.

In portable code, you use EXIT_SUCCESS and EXIT_FAILURE. In
less than portable code, you use values which are common on all
targeted platforms: 0 for success and a small, positive integer
for failure is portable between Unix and Windows, for example.

Internally, I use the following convention:

0 Success, in every sense of the word
1 No error, but not the expected results (e.g. something like
grep matching no lines)
2 An error occured some where during processing.
3 A fatal error occured; processing was interrupted by the
error.

Formally, I also define a 5 for internal errors, but these never
make it to exit -- abort() gets called before then.

These are mapped in an implementation defined manner to the
actual return code; under Unix and Windows, the mapping function
is the identity function, but the default mapping function (for
an unknown system) maps 0 to EXIT_SUCCESS, and everything else
to EXIT_FAILURE.

In practice, this is fairly arbitrary (although the 0/1/2 is
based on Unix grep), and almost all invoking applications will
treat anything but 0 as an error.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Allan W
Guest





PostPosted: Thu Oct 20, 2005 11:21 am    Post subject: Re: use of exit(-1) Reply with quote

Carl Barron wrote:
Quote:
I don't recommend exit() in new code,

Why not?

My own programs typically don't call exit() -- they simply return to
main, which executes return 0. Or, if a critical error is detected,
I call abort(). In cases where I want to control the return value,
I'm likely to use implementation-defined code, returning a value
from 0 to 255 (which can be seen by i.e. BATCH files under
Microsoft Windows).

But this is just the way I happen to organize the program. If I
knew that at a certain point in the code, all processing was 100%
finished, including RAII from statics and globals, I would have
no reason not to call exit.

Quote:
a try block in main
catching a specific exception containing the int that would
be passed to exit() [throwing it instead of calling the function
exit(). ] then the cach block could just return the contained
integer from main and all gets destructed properly.

something like this is better C++ code to emulate exit(n)
unless you really do want non destructed objects on quitting.

struct exit_now
{
int ret_val;
exit_now(int a):ret_now(a){}
};

What is ret_now? I'm sure you mean ret_val.

Quote:
int main()
{
try
{
//....
} catch(exit_now &x)
{
return x.ret_val;
}
return 0;
}

Then the usage would be:
throw exit_now(EXIT_FAILURE);

But I still don't see the advantage.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Thu Oct 20, 2005 5:33 pm    Post subject: Re: use of exit(-1) Reply with quote

In article <1129738923.947263.51650 (AT) o13g2000cwo (DOT) googlegroups.com>,
Allan W <allan_w (AT) my-dejanews (DOT) com> wrote:

Quote:
Carl Barron wrote:
I don't recommend exit() in new code,

Why not?

My own programs typically don't call exit() -- they simply return to
main, which executes return 0. Or, if a critical error is detected,
I call abort(). In cases where I want to control the return value,
I'm likely to use implementation-defined code, returning a value
from 0 to 255 (which can be seen by i.e. BATCH files under
Microsoft Windows).

But this is just the way I happen to organize the program. If I
knew that at a certain point in the code, all processing was 100%
finished, including RAII from statics and globals, I would have
no reason not to call exit.

a try block in main
catching a specific exception containing the int that would
be passed to exit() [throwing it instead of calling the function
exit(). ] then the cach block could just return the contained
integer from main and all gets destructed properly.

something like this is better C++ code to emulate exit(n)
unless you really do want non destructed objects on quitting.

struct exit_now
{
int ret_val;
exit_now(int a):ret_now(a){}
};

What is ret_now? I'm sure you mean ret_val.

int main()
{
try
{
//....
} catch(exit_now &x)
{
return x.ret_val;
}
return 0;
}

Then the usage would be:
throw exit_now(EXIT_FAILURE);

But I still don't see the advantage.

The difference is with exceptions the stack unwinds and the objects on

the stack become destructed as if the block was exited until the catch
clause is found. Exit() is a function call and it just jumps someplace
that exits the process. That is the difference! A big difference if
you
use anything but pointer free pods for automatic objects!

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.