[comp.sys.sun] exit

gbarker@mph.sm.ucl.ac.uk (Dr Gareth J. Barker) (08/08/90)

Can somebody explain to me what the lint message below means?

	titan% cat test.c
	main()
	{
	}
	titan% lint test.c
	test.c(3): warning: main() returns random value to invocation
	environment
	titan% 
    titan% cat test1.c
    void exit();

	main()
	{
		exit(0);
	}
	titan% lint test1.c
	test.c(6): warning: main() returns random value to invocation environment

Replacing exit by return makes lint happy.

	titan% cat test2.c
	main()
	{
		return(0);
	}
	titan% lint test2.c
	titan% 

Is lint just trying to tell me that any function, including main, must
have at least one 'normal' return?  Is this a new feature in 4.1, I
don't remember seeing this before?

-- 
Gareth J. Barker,
Institute of Neurology, Queen Square,
London, UK.

JANET    : gbarker@uk.ac.ucl.sm.mph
INTERNET : gbarker@.mph.sm.ucl.ac.uk
BITNET   : gbarker%uk.ac.ucl.sm.mph@ukacrl.bitnet

richard@unipalm.co.uk (08/13/90)

> 	titan% cat test.c
> 	main()
> 	{
> 	}
> 	titan% lint test.c
> 	test.c(3): warning: main() returns random value to invocation
> 	environment

Your declaration of main is taken by the compiler to mean that it returns
the default type (int). Therefore no return statement is not allowed, and
will indeed return a random value to the calling environment.

Adding an exit line does not help lint, it has no way of knowing that the
exit function actually exits.  Lint has various mechanisms to allow the
programmer to tell lint information about the program. A case of RTFM
mainly.

In this particular case, if your main function ever returns, you should
have a return statement, if it always exits, you should tell lint that it
will never get to the end of the routine.

e.g.

/*ARGSUSED*/
int main(argc, argv)
int argc;
char **argv;
{
...
   return(some_value);
}

or

/*ARGSUSED*/
int main(argc, argv)
int argc;
char **argv;
{
...
   exit(0);
   /*NOTREACHED*/  /* tells lint that this line is never reached */
}


Richard Nuttall                                richard@xtech.uucp
XTech, Cambridge, England                      ukc!acorn!unipalm!xtech!richard
Tel: +44 954 211862                            richard@unipalm.uucp

gbarker@mph.sm.ucl.ac.uk (Dr Gareth J. Barker) (08/13/90)

The answer to my question :

was that main is implicitely defined as returning an int (since I hadn't
defined it as anything else) and lint therefore wanted to se it return an
int at the end any path through the routine.  Since it doesn't know that
exit() never returns it give an error message when it 'drops off the end'
of the function.  The solution is either to change the code to:

		main()
		{
			exit(0);
			return(0);
		}

where the return() statement will never be reached but will keep lint
happy, or, more cleanly:

		main()
		{
			exit(0);
			/*NOTREACHED*/
		}

Thanks to:
kucharsk@com.solbourne  
cudcv@uk.ac.warwick.cu  
sanzgiri%flamingo.meta  
steve@com.taumet       
cgwst@edu.pitt.cis.uni
guy@com.auspex       
scl@edu.virginia.acc.s  
cs@com.sun.eng         
lwv27@EARN.CAS        
(David Moore)moore%ess
hue%island@com.sun    

Gareth J. Barker,
Institute of Neurology, Queen Square,
London, UK.

JANET    : gbarker@uk.ac.ucl.sm.mph
INTERNET : gbarker@.mph.sm.ucl.ac.uk
BITNET   : gbarker%uk.ac.ucl.sm.mph@ukacrl.bitnet