Question: Overflow

  • Thread starter Michael Thomas Cassady
  • Start date
M

Michael Thomas Cassady

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?

The following code appears to exit abruptly:

#include <stdio.h>

void a();
void b();

int main(int argc, char *argv[])
{
a();
printf ("Hello World!\n");
getchar();
}

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
 
W

Walter Roberson

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

On almost all architectures, calls to functions need to store the
return address somewhere, and eventually that space will run out
if you have infinite recursion.
 
F

Flash Gordon

Michael said:
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".
 
M

Malcolm

Michael Thomas Cassady said:
The following code appears to exit abruptly:

#include <stdio.h>

void a();
void b();

int main(int argc, char *argv[])
{
a();
printf ("Hello World!\n");
getchar();
}

void a()
{
b();
printf("H\n");
}

void b()
{
a();
}
You are causing infinite recursion, which no computer can handle.
A decent operating system will terminate the program with an error message
telling the user what has happened, whilst a lousy one will just exit, or
crash.
C doesn't define what will happen under such circumstances.
 
A

Alan Balmer

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?

The following code appears to exit abruptly:

#include <stdio.h>

void a();
void b();

int main(int argc, char *argv[])
{
a();
printf ("Hello World!\n");
getchar();
}

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?

Sure you do. For one thing, it's probable that the return address is
on the stack, though that's implementation dependent. Also, the
implementation is free to use the stack to keep any other
per-invocation data it might find handy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

overflow problem? 6
Integer Overflow 121
Command Line Arguments 0
THE PROGRAM IS NOT RUNING 3
Using Do-While loop statement for a selection Menu 3
Beginner at c 0
Integer Overflow 18
First time question 1

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top