Path: utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!ucsd!ames!haven!mimsy!chris From: chris@mimsy.umd.edu (Chris Torek) Newsgroups: comp.lang.c Subject: Re: main() arguments, was Re: typedef-ing an array Message-ID: <25312@mimsy.umd.edu> Date: 5 Jul 90 16:17:00 GMT References: <78627@srcsip.UUCP> <78633@srcsip.UUCP> <25247@mimsy.umd.edu> <4241@jato.Jpl.Nasa.Gov> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 53 >In article <25273@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@jato.Jpl.Nasa.Gov> kaleb@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@cs.umd.edu Path: uunet!mimsy!chris