Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with sizeof in while loop reading a file

Reply
Thread Tools

problem with sizeof in while loop reading a file

 
 
Richard Tobin
Guest
Posts: n/a
 
      09-15-2007
In article <(E-Mail Removed)>,
CBFalconer <(E-Mail Removed)> wrote:

>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
 
=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-15-2007
On Sat, 15 Sep 2007 20:34:04 +0000, Richard Tobin wrote:
> In article <(E-Mail Removed)>, Keith Thompson
> <(E-Mail Removed)> wrote:
>
>>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?
>
> 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].
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      09-15-2007
In article <fchh6u$f0i$(E-Mail Removed)1.ov.home.nl>,
$)CHarald van D )& k <(E-Mail Removed)> wrote:

>>>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().

>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-16-2007
On Sat, 15 Sep 2007 22:43:00 +0000, Richard Tobin wrote:
> In article <fchh6u$f0i$(E-Mail Removed)1.ov.home.nl>, $)CHarald van D )& k
> <(E-Mail Removed)> wrote:
>
>>>>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. 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.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-16-2007

"Harald van D?k" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fcj02p$qn5$(E-Mail Removed)1.ov.home.nl...
> On Sat, 15 Sep 2007 22:43:00 +0000, Richard Tobin wrote:
>> In article <fchh6u$f0i$(E-Mail Removed)1.ov.home.nl>, $)CHarald van D )& k
>> <(E-Mail Removed)> wrote:
>>
>>>>>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


 
Reply With Quote
 
=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-16-2007
On Sun, 16 Sep 2007 12:31:59 +0200, Joachim Schmitz wrote:
> "Harald van D?k" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:fcj02p$qn5$(E-Mail Removed)1.ov.home.nl...
>> 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.


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.)
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-16-2007
On Sat, 15 Sep 2007 20:22:20 +0200, Bart van Ingen Schenau wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> I was curious about that. How do you know when to use "return 0;" vs
>> "exit(0);" and does it make a difference?

> 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.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments