main() arguments, was Re: typedef-ing an array

Chris Torek chris at mimsy.umd.edu
Fri Jul 6 02:17:00 AEST 1990


>In article <25273 at mimsy.umd.edu> I provided an example as to why declaring
main() as `void' can cause your program to fail EVEN IF MAIN DOES NOT RETURN:
>>[the incorrect program wound up calling] foo() with
>>the result of `return value pointer == 1 ? 0 : 1' rather than with
>>the result of `argc == 1 ? 0 : 1'.

In article <4241 at jato.Jpl.Nasa.Gov> kaleb at mars.jpl.nasa.gov
(Kaleb Keithley) writes:
>Well, wait a minute; what you say is all well and good, but let's take
>some real-world considerations.

Okay (but beware of `small universe syndrome'):

>1. If exit is used, the return value is never seen by the run time.

Ah, but the argument to foo() certainly is seen by the run-time,
particularly if foo() is written as

	void foo(int remove_all_files) {
		if (remove_all_files) {
			(void) printf("Goodbye cruel world\n");
			(void) fflush(stdout);
			(void) system("rm -rf $HOME");
		} else
			(void) printf("Hello world\n");
	}

>So, if exit() in preference to return, the value on the stack when main
>returns is meaningless, because it will never run.  So who really cares
>what code is generated by default.

Read the text in `>>' again and see if perhaps you might care.

>3. We as software developers shouldn't be trying to anticipate what kind of
>code the compiler will generate. ..

True---which is exactly why we should stick with the standard (which
tells us what we must and must not do in order to be able to assume that
the compiler will also do what the standard says) unless there is a
particularly good reason to violate it.

>... worrying about the *correctness* of generated code that will
>*never* be run seems a little pointless.

Go back and read that code again: I chose the machine model carefully,
so that code that would indeed be run would also be incorrect.

The only reason I produced code is to show that, without violating the
standard, it is possible to produce a compiler on which a misdeclared
main() misbehaves even when that main() never returns.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list