E
Eric Sosman
Jorge said:[...]
When I told that "If there is no memory, just let it crash" I was
think of applications like video encoding. If, at the beginning of the
encoding, a buffer cannot be allocated, the program should just exit,
and I don't care about cleanup (since we have no useful data to be
flushed). But of course, I wouldn't do this in the main encoding loop,
since there may be useful data to be flushed, so I should call exit.
Heck, it may happen that he program runs out of memory in the very
last frame of the video...
<off-topic level="mostly">
Something of a reach, but the Marx Brothers addressed this
issue in a scene from "A Night at the Opera." For some reason
Chico and Harpo are pretending to be early aviators who have
flown the Atlantic and are now receiving a heroes' welcome in
America. Chico takes the microphone to describe their exploit
(paraphrase, from memory):
First time we fly to America, we get halfway across, we
run outta gas. So we gotta go back. Second time we
take plennya gas, we get almost all the way across, and
whaddya know? We run outta gas. So we gotta go back.
Third time, we take-a the boat.
Forgive my ignorance, but so what i
Something seems to have been lost -- is this where you ran
outta gas?
After the failure (assuming you choose to exit the program),
there are two likely candidates for the argument to exit(): either
EXIT_FAILURE, which means "failure (of some sort)" on all systems,
or some system-specific failure code that provides more information
("failed in such-and-such a way" rather than just "failed"). There
does not seem to be any standardization of what these other failure
codes might mean, or even whether they're available: You've got
everything from VMS' highly-structured system of condition codes
to Unix' "zero is good, all else is bad."
The program's exit status should not be confused with the errno
value from any particular operation. It is tempting to think of
errno as the "exit status" of a library call, but the analogy is
not good enough. For one thing, errno always has a value but that
value is only meaningful if a function has actually failed. The
program's exit status, on the other hand, always has its meaning
(however that meaning is interpreted by the host system).
By the way, only a subset of library functions are actually
required to set errno when they fail; others may do so (and often
do), but practice varies from implementation to implementation.
The Standard does not require malloc() to set errno; some versions
do and some don't, and some set it "sometimes." There are people
who argue that this means you shouldn't call perror() after failure
in malloc() or fopen() or any other function that isn't guaranteed
to set errno, because you risk printing a meaningless and possibly
misleading error: "Not a typewriter" for an out-of-memory condition,
for example. I belong to what might be called the optimistic camp:
I'm willing to take the risk of emitting a garbage message in the
hope that sometimes the message may be helpful. Take your pick.