Mail Archives: djgpp/1996/07/06/17:50:50
Reply to message 6380707 from TOMW AT TSYS DOT DEM on 07/05/96 7:14PM
>From my experience on comp.lang.c, you are misreading this. The paragraph
>means that if you just fall off the end of main(), without using return
>or exit(), *then* the return value is undefined. This is different from
>the undefined *behaviour* of void main. The first can make your OS do
>funny things, the second your program.
I don't follow comp.lang.c, but in my understanding, a function which returns
a 2- or 4-byte value does so by default in (e)ax, which is where its caller
would
expect to find such a value. If a function which ordinarily returned int (such
as
main) were redeclared to return void, then this would not necessarily have
any effect on the compiler itself. However, when the void function in question
returns, any function which expected a return value would see whatever
happened to be in eax at the time. Offhand, I can see several places where
this kind of behavior of main() could causeproblems:
1) The OS exit code is formed by the return value of main; therefore in
any program that declares main as void the exit code it returns will be
completely random, invalidating any batch file or script which depends
on this.
2) Similarly, any attempt to execute the aforementioned program with
system() or spawn*() would receive an unpredictable return value,
confusing any program which does this.
Now, for the most part this isn't particularly significant, as exit codes are
discarded 90% of the time. But a programmer who gets into the bad
habit of always making main() return void could potentially get some very
nasty surprises
>This statement is immediately followed by the example:
> void main (void)
>even though the text of the standard directly opposite states that this is
>undefined. Indeed, the text I quote makes me wonder whether Schildt believes
>that:
>
> struct foo { int i; double d; } main (double argc, struct foo argv)
>
>is permitted !
Actually, theabove would compile unless you invoke gcc with options
to force extreme strictness to ANSI compatibility (in DJ's words, make it
a hard-ass :) ). However, the parameters of main as formed at run time
would be passed in the standard format, as (int, char**), and there's no
telling how this would muck up the nastiness above. Secondly, anything
that expected a return value from main() would probably receive the
address of the struct foo that main is declared to return, which would
probably not make them particularly happy either. :)
John
- Raw text -