Newsgroups: comp.os.minix
Subject: Re: C program
References: <H7l96.73870$eB2.5866586@news.infostrada.it> <slrn96d9ib.baj.pino+comp_os_minix@mud.stack.nl> <slrn96da17.bln.stian@localhost.localdomain> <slrn96daui.bs7.pino+comp_os_minix@mud.stack.nl>
Organization: Rochester Institute of Technology, Rochester, NY
From: aje9383@osfmail.isc.rit.edu (Andrew Erickson)
NNTP-Posting-Host: grace.isc.rit.edu
X-Original-NNTP-Posting-Host: grace.isc.rit.edu
Message-ID: <3a67702e@news.isc.rit.edu>
Date: 18 Jan 2001 17:37:34 -0500
X-Trace: 18 Jan 2001 17:37:34 -0500, grace.isc.rit.edu
Lines: 49
XPident: aje9383
X-Original-NNTP-Posting-Host: 129.21.4.100
XPident: Unknown
Path: news.adfa.edu.au!clarion.carno.net.au!news0.optus.net.au!news1.optus.net.au!optus!news.mel.connect.com.au!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsswitch.lcs.mit.edu!bloom-beacon.mit.edu!news.kodak.com!news-nysernet-16.sprintlink.net!news.sprintlink.net!news.isc.rit.edu!aje9383
Xref: news.adfa.edu.au comp.os.minix:36594

In article <slrn96daui.bs7.pino+comp_os_minix@mud.stack.nl>,
Martijn van Buul <pino+comp_os_minix@dohd.org> wrote:
>It occurred to me that Stian Sletner wrote in comp.os.minix:
>> * Martijn van Buul wrote:
>> : 
>> | retro-active counterattack: If you don't need the return value, why
>> | including it?

main should be defined properly because the runtime startoff routines
(logically, if not in actuality) are compiled with a specific declaration. 
It's a generalization of the rule that all functions should have definitions
which match their declarations, and all parameters should be cast to the
appropriate types if they aren't already of that type (preferably explicitly
cast, as implicit casting by ANSI prototypes falls apart if the headers are
old or if variable arguments are introduced, and one is bound to omit a
needed cast if one isn't in the habit of already).

>> `void main()' is not allowed in C, it causes undefined behaviour, which
>> may lead to nasal demons and other horrendeous things.
>
>Well, int main(void) is equally not-allowed. Main gets arguments passed
>to it, wether you like it or not. Note that this doesn't much harm in 
>this case, but calling a function with void arguments with an argument
>(hmm. So much for creating a nice sentence;) does trigger an error
>normally. It does generate stack corruption. 
>
>void main() may lead to undefined results, but it leaves the stack alone.
>As far as I know, an int-result gets returned in one of the registers.
>Since this is a void function, it will return some leftovers, but at least
>the stack is left alone.

What happens with the stack is *very* *much* a detail of the implementation,
influenced by the particular processor architecture used.  On must systems,
an int is returned in a register, but there's no reason why it couldn't be
done on the stack; and on, say, a 6502 system (there are C compilers for
them, indeed), I would quite expect it would be done that way  (The 6502 has
only one eight-bit accumulator and two eight-bit index registers; you'd have
to use the accumulator and one of the index registers to return a (16-bit)
int, which would be a pain.)

In general, passing extra unused parameters will cause no harm, as virtually
every C compiler I know of makes the caller responsible for cleaning up the
parameters passed on the stack.  This is probably because it's much easier
to handle variable argument lists this way than in a callee-cleanup
approach.  Still, this is an implementation detail, and might not be true
for every system.

-- 
Andrew Erickson
