void main() (second proof of non-portability)

bdm659 at csc.anu.oz bdm659 at csc.anu.oz
Sat Nov 18 03:02:48 AEST 1989


 In article <7733 at cdis-1.uucp>, tanner at cdis-1.uucp (Dr. T. Andrews) writes:
> [[The question is: why is it wrong to declare main() as void, instead of
> int, even though he program is terminated by calling exit() explicitely?]]
>
> I should like to hear details of a case where the code will not
> work.  It seems ridiculous to declare un-portable the results of a
> void-vs-int return linkage which is never made, and I should like
> to hear of an implementation where this actually fails.
>
> If no real-world example is available, I should be almost as
> satisfied with a conjectural example (clearly marked as conjecture).

I've thought of an even simpler hypothetical implementation for which
declaring main() as void fails to work.  This one may even exist (anyone?).

Suppose the implementation of calling a function goes like this:
* push the arguments onto the stack
* move the stack pointer some more to make some room for the function to
  return its value in
* branch to the function
The called function gets its arguments by indexing off the stack pointer.

Now, if the calling function has the wrong idea about what the type of the
function being called is, the called function will not find its arguments
correctly.  Thus, if you declare main() as void, you can't expect to be
able to use argv and argc.  It makes no difference whether you are leaving
main() by doing return() or exit().

Brendan McKay.  bdm at anucsd.oz.au  or  bdm659 at csc1.anu.oz.au



More information about the Comp.lang.c mailing list