[NTLUG:Discuss] OT: C question
Douglas Scott
dcscott1 at comcast.net
Wed May 10 20:18:38 CDT 2006
On Wednesday 10 May 2006 5:56 pm, anindya Chakraborty wrote:
> On 5/10/06, Fred James <fredjame at fredjame.cnc.net> wrote:
> > All
> > I have a C program that if compiled with a "return (0);" as the last
> > line in main(), runs correctly but give a "Bus error(core dump)" message.
> > If I compile the same C program with a "exit (0);" as the last line in
> > main(), the run is also correct, and there is no error message.
> >
> > What is the difference, please?
>
> are you having the declaration of main as int main() ? if the return time
> is main then you should not have any problem with returning 0 with
> return(0). But, exit causes the program to end and supplies a status code
> to the calling environment.
It might be a declaration problem, but I am thinking of a stack error.
If you use return to exit main then the return address is taken from the
stack. If you managed to trash the stack then the return address will have
been overwritten. The program will most likely try to jump to a place in
memory outside the process's memory area. I am pretty sure I have seen bus
errors from that sort of thing in the past.
If you use exit() to exit from a process then it calls functions that destroy
and cleanup the current process without retrieving information from the stack
of main(). So even with a damaged return address exit() would probably make
a clean getaway.
You wouldn't happen to have a string declared in main() that you copied
something into without checking the length?
Doug
More information about the Discuss
mailing list