Aborting

S

Stefan Ram

Assuming that a program is in such a desperate situation,
that one wants to abort immediatly (for example, no more
memory is available), when should one use

exit( 99 )

(where »99« is a placeholder for any possible error code) and when

abort()

?
 
L

Lew Pitcher

Assuming that a program is in such a desperate situation,
that one wants to abort immediatly (for example, no more
memory is available), when should one use

exit( 99 )

(where »99« is a placeholder for any possible error code) and when

abort()

I would suspect that the decision to use abort() or exit() would depend a
lot on the circumstances of the error, the goals of the application, and
the QoI of the C implementation.

abort()
- does not guarantee to flush open buffered output streams,
- does not guarantee to close open streams of any sort,
- does not guarantee to remove temporary files, and
- returns an implementation-defined "unsuccessful termination" status.

OTOH, exit()
- executes the functions registered through the atexit() function
- flushes open buffered output streams
- closes all open streams
- removes temporary files, and
- returns a program-specified exit status to the host environment

I'd use abort() only in the direst of circumstances, where the program logic
cannot guarantee the sanity of /any/ of it's data. For all other
terminations, I'd use exit().

And, I'd document the use carefully, including expected behaviour and
recovery options.

HTH
 
B

Ben Pfaff

Assuming that a program is in such a desperate situation,
that one wants to abort immediatly (for example, no more
memory is available), when should one use

exit( 99 )

(where »99« is a placeholder for any possible error code) and when

abort()

On Unix-like systems, abort() will often cause a core dump, but
exit() will not. It is usually much easier to find a difficult
bug with a core dump than without one, so that's part of the
criteria that I use.

Another way to look at it is that the assert macro exits by
calling abort(), so that it is appropriate to call abort() in
circumstances comparable to assertion failures.
 
F

frank

Lew said:
I would suspect that the decision to use abort() or exit() would depend a
lot on the circumstances of the error, the goals of the application, and
the QoI of the C implementation.

abort()
- does not guarantee to flush open buffered output streams,
- does not guarantee to close open streams of any sort,
- does not guarantee to remove temporary files, and
- returns an implementation-defined "unsuccessful termination" status.

Is this last one synonymous with the requirement that __FILE__ and
__LINE__ be given?
OTOH, exit()
- executes the functions registered through the atexit() function
- flushes open buffered output streams
- closes all open streams
- removes temporary files, and
- returns a program-specified exit status to the host environment

I'd use abort() only in the direst of circumstances, where the program logic
cannot guarantee the sanity of /any/ of it's data. For all other
terminations, I'd use exit().

And, I'd document the use carefully, including expected behaviour and
recovery options.

An interesting contrast, Lew. Which temporary files do you mean?
 
K

Keith Thompson

frank said:
Lew Pitcher wrote: [...]
abort()
- does not guarantee to flush open buffered output streams,
- does not guarantee to close open streams of any sort,
- does not guarantee to remove temporary files, and
- returns an implementation-defined "unsuccessful termination" status.

Is this last one synonymous with the requirement that __FILE__ and
__LINE__ be given?

What? No, it's completely unrelated.

The assert() macro writes the values of__FILE__ and __LINE__ to
stderr. There is no such requirement for abort(). And even if there
were, it would have nothing to do with the termination status.

[...]
 
N

Nick Keighley

Lew said:
On January 18, 2010 15:27, in comp.lang.c, (e-mail address removed)-berlin.de wrote:
abort() [...]
 - returns an implementation-defined "unsuccessful termination" status.

Is this last one synonymous with the requirement that __FILE__ and
__LINE__ be given?

no. The "unsuccessful termination" status is some sort of indication
to the underlying OS. Typically an integer indicating failure. abort()
is not required to give file and line number. Typically it doesn't.

OTOH, exit() [...]
 - removes temporary files
[...]
[...]  Which temporary files do you mean?

ones generated by the tmpfile() function (maybe others?)
 
E

Ersek, Laszlo

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top