[comp.lang.c] WEIRD BUG ABOUT EXIT

brian@orange.ucsb.edu (Brian Mak) (07/30/90)

I encounter a weird bug at the end of my program. I got a segmentation fault
right at the closing bracket of the main(). i.e. if

line 1:	main()
line 2:	{
	 .
	 .
	 .
line N:	}

the faults occurs right at line N.

But if I put exit(0) before line N, it exits without error!

The only reason I may think of is that the address holding STOP of the 
main() is somehow overwritten by the program. But does anyone of you may
think of probable reasons for that? Or does anyone has encountered such
error before?

Please reply both to me personally and to the newsgroup.

Brian MAK

steve@taumet.com (Stephen Clamage) (07/30/90)

brian@orange.ucsb.edu (Brian Mak) writes:

>I encounter a weird bug at the end of my program. I got a segmentation fault
>right at the closing bracket of the main(). i.e. if
>line 1:	main()
>line 2:	{
>	 .
>line N:	}
>the faults occurs right at line N.
>But if I put exit(0) before line N, it exits without error!

Two possibilities occur to me:

1.  There is an error in your program causing the stack to be overwritten,
so that the return from main() doesn't work properly.  Calling exit()
directly avoids the problem area.  The usual error is a wild pointer
of some kind:  uninitialized pointer, error return not checked for and
bogus pointer value used, off-by-one in a loop or array, non-portable
assumption about pointers or addresses which was false on this system.
With that checklist, you should have no trouble isolating the error :-).

2.  Your C runtime environment is nonstandard, in that returning from
main() doesn't work.  This seems very unlikely, but is easy to check.
See what your manual says about returning from main(), and/or try the
"hello world" program with no explicit return from main().
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com