problem with sizeof in while loop reading a file

R

Richard Tobin

CBFalconer said:
Use return in main. exit exits the complete program, regardless of
where called. Both can only take the arguments 0, EXIT_SUCCESS,
EXIT_FAILURE.

No, they can take any int value. The only special feature of the
values you list is that they have specified effects on the host
environment, while others have an implementation-defined effect.

-- Richard
 
?

=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F

Can you cite chapter and verse for this?

According to C99 7.20.4.2, the registered functions are called at normal
program termination. According to 7.20.4.3, exit() causes normal
termination to occur. I don't see anything that prevents exit() from
causing normal termination by longjmp()ing to main()'s caller, which
would then handle the atexit() functions.

It's in 7.20.4.3p3, immediately following the p2 you read:

*First*, all functions registered by the atexit function are called,
[...]

Or, for something more explicit but non-normative, see the footnote
attached to 5.1.2.2.3:

In accordance with 6.2.4, the lifetimes of objects with automatic
storage duration declared in main will have ended in the former case
[returning from main], even where they would not have in the latter
[calling exit].
 
R

Richard Tobin

Can you cite chapter and verse for this?
[/QUOTE]
It's in 7.20.4.3p3, immediately following the p2 you read:

*First*, all functions registered by the atexit function are called,
[...]

I took that sequence as specifying the relative order of the events
it lists - which are the the things required of "normal termination" -
rather than an exhaustive list of what happens in exit().
Or, for something more explicit but non-normative, see the footnote
attached to 5.1.2.2.3:

In accordance with 6.2.4, the lifetimes of objects with automatic
storage duration declared in main will have ended in the former case
[returning from main], even where they would not have in the latter
[calling exit].

That doesn't seem unambiguous to me: "even where" implies that it is
not necessarily the case. And I see that the (equally non-normative)
Rationale says (7.20.4.3):

Aside from calls explicitly coded by a programmer, exit is invoked
on return from main. Thus in at least this case, the body of exit
cannot assume the existence of any objects with automatic storage
duration except those declared in exit.

"At least" again suggests that main() may have returned when exit() is
called explicitly.

-- Richard
 
?

=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=

It's in 7.20.4.3p3, immediately following the p2 you read:

*First*, all functions registered by the atexit function are called,
[...]

I took that sequence as specifying the relative order of the events it
lists - which are the the things required of "normal termination" -
rather than an exhaustive list of what happens in exit().

I could say the same about printf, meaning

#include <stdio.h>
int main(void) {
int i = 0;
printf("%d\n", i);
printf("%d\n", i);
return 0;
}

might print 0 and 1, because the description of printf doesn't say i
won't be modified. Clearly, this doesn't work. The description of a
function is a complete description. The description of exit describes all
the steps performed in normal program termination. If exit longjmps to
outside of main, it must be one of the three steps, and given only these
three steps, it's not possible for it to occur before the atexit
registrations are called.
Or, for something more explicit but non-normative, see the footnote
attached to 5.1.2.2.3:

In accordance with 6.2.4, the lifetimes of objects with automatic
storage duration declared in main will have ended in the former case
[returning from main], even where they would not have in the latter
[calling exit].

That doesn't seem unambiguous to me: "even where" implies that it is not
necessarily the case.

Indeed:

#include <stdlib.h>
int main(void) {
{ int i; }
/* i is declared in main, but no longer live when exit is called */
exit(0);
}
And I see that the (equally non-normative)
Rationale says (7.20.4.3):

Aside from calls explicitly coded by a programmer, exit is invoked on
return from main. Thus in at least this case, the body of exit cannot
assume the existence of any objects with automatic storage duration
except those declared in exit.

"At least" again suggests that main() may have returned when exit() is
called explicitly.

I don't believe there's any prohibition against a function registered
with atexit calling exit again (though it should not do so
unconditionally, for obvious reasons, and preferably should not do so at
all). Additionally, it may refer to implementation extensions to call
code after returning from main without help of atexit.
 
J

Joachim Schmitz

Harald van D?k said:
$)CHarald van D )& k said:
A function can be registered with 'atexit' so that it will be invoked
when the program terminates. If you call exit() from main, such
functions will be called before the main function terminates,
Can you cite chapter and verse for this?
It's in 7.20.4.3p3, immediately following the p2 you read:

*First*, all functions registered by the atexit function are called,
[...]

I took that sequence as specifying the relative order of the events it
lists - which are the the things required of "normal termination" -
rather than an exhaustive list of what happens in exit().

I could say the same about printf, meaning

#include <stdio.h>
int main(void) {
int i = 0;
printf("%d\n", i);
printf("%d\n", i);
return 0;
}

might print 0 and 1, because the description of printf doesn't say i
won't be modified.
How could printf possibly modify i? C is doing call by value, after all.

bye, Jojo
 
?

=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=

How could printf possibly modify i? C is doing call by value, after all.

Compiler magic, or corrupting random bytes of memory.

(Obviously, printf is not allowed to modify i. That was the point, and I
think you got it, but it's worth restating.)
 
A

Army1987

It will not really make a difference.
The start-up code that gets executed right before main() might have the
equivalent of
exit(main(argc, argv));
in it.
Therefore, if a function registered by atexit() somehow refers to
an automatic variable of main()... Ka-Boom! In that case, you will
have to call exit() *within* main(). Note that the standard's
wording doesn't *seem* to make this point, weren't it for a
(non-normative) footnote it would be extremely likely to be
misunderstood.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top