Chris Dollin said:
christian.bau said:
There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.
That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...
[1] As in "oh noes, my brain just melted".
I thought I had come up with a plausible example, but I don't think
I actually have.
Suppose main() saves copies of argc and argv in "global" variables
saved_argc and saved_argv, so they can be accessed by other functions.
An atexit-registered cleanup routine uses these to access the
command-line arguments for whatever reason.
But then I looked at C99 5.1.2.2.1p2:
If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers to
strings, which are given implementation-defined values by the host
environment prior to program startup.
[...]
The parameters argc and argv and the strings pointed to by the
argv array shall be modifiable by the program, and retain their
last-stored values between program startup and program
termination.
The strings pointed to by the argv array retain their values until
*program termination*, not just until termination of the main()
function.
If a program saved *pointers to* argc and argv, then there could be a
problem, but I can't think of any good reason to do that rather than
saving copies of them.
The standard is vague about the argv array itself (i.e., the array of
char* to whose first element the argv parameter points). It *should*
say that this array also survives until program termination, and I
think it's reasonably safe to assume that it does (other than on the
DS9K).
(Question: Does the DS9K implementation exploit poor wording, or does
it just exploit freedoms deliberately offered by the standard to
absurd extremes?)