Michael Thomas Cassady wrote:
I know my subject wasn't the best but I hope people still look at this.
I know that this is probably compiler and implementation dependent and
that this is a standard c discussion group but can someone point me in
the right direction or just answer?
I would say that the problems with your code are on topic. Although it
could be argued that the problem is your lack of understanding of recursion.
The following code appears to exit abruptly:
#include <stdio.h>
void a();
void b();
It would be better to specify the functions don't take any parameters so
that the compiler has to complain if you erroneously pass parameters.
void a(void);
void b(void);
int main(int argc, char *argv[])
You don't use the parameters, so you might as well use the alternative
valid definition so the human reader can immediately see this.
int main(void)
{
a();
printf ("Hello World!\n");
getchar();
Main returns an int so it would be better to actually return one! The
C99 standard will do implicitly return a 0, but for the want of a few
characters why not be more compatible and return one explicitly?
return 0;
}
void a()
{
b();
printf("H\n");
}
void b()
{
a();
}
Now, I know it cuts off abruptly because in Windows Xp the console
disappears almost instantly, prints nothing out and wiats for no input
at the getchar(). Hence it must abruptly end somewhere in the infinite
recursion of calls, but my question is this:
is this a call stack overflow error? If so, why? I don't pass any
variables, return any variables or have any local variables to store on
the stack... why is it doing this? Thanks
What about the return address? In general that has to be stored
somewhere, although in your case an optimising compiler could avoid that
by just doing an infinite loop...
What you have told the compiler you want to happen is...
main calls a
a calls b
b calls a
a calls b
b calls a
and so on for ever. It never reaches the printf in a because it always
recurses before it gets to it, and it never reaches the printf in main
because a never returns. I would suggest reading the chapters on
recursion in your text book, including anything that mentions "mutually
recursive functions" or "mutual recursion".
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.