473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question: Overflow

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
Nov 15 '05 #1
4 1416
In article <df**********@ruby.cit.cornell.edu>,
Michael Thomas Cassady <mt***@cornell.edu> wrote:
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.

--
Any sufficiently old bug becomes a feature.
Nov 15 '05 #2
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.
Nov 15 '05 #3

"Michael Thomas Cassady" <mt***@cornell.edu> wrote
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.
Nov 15 '05 #4
On Thu, 08 Sep 2005 19:04:52 -0400, Michael Thomas Cassady
<mt***@cornell.edu> 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?

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.
Thanks

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pxlpluker | last post by:
I have a friend that was using formmail.pl until last weekend when it was hacked by spammers with a buffer overflow attack on one of the form fields. I don't know enough about perl to know...
2
by: Adelard | last post by:
Here is my xsl and xml, I want the xml node: LABEL3 ( General Information ) to move under the ROW when viewing or printing this PDF Document. How can I do it? Thanks
13
by: fleimeris | last post by:
hello, why do we need explicit cast here ? short i = 0; //i = i + 1; // Cannot implicitly convert type 'int' to 'short' //i = i + (short)1; // Cannot implicitly convert type 'int' to 'short' i...
17
by: vashwath | last post by:
#include <stdio.h> int main() { FILE *fp; char s; fopen("file.txt","w+"); fprintf(fp,"HI\n");
2
by: sri | last post by:
Dear All, I would like to implement file buffer class to make avialable buffering mechanism to ifstream or own customized file stream. My question is what are the semantics for seekoff,...
4
by: zzpat | last post by:
I have a box element that uses overflow: hidden and I want a link which at the bottom of the element to be visible. I can't make the link visible. Obviously I'm having problems so I was...
39
by: Juha Nieminen | last post by:
I was once taught that if some integral value can never have negative values, it's a good style to use an 'unsigned' type for that: It's informative, self-documenting, and you are not wasting half...
22
by: Gio | last post by:
short i, j; i = -32768; j = -i; printf("%d", j); As some of you may have guessed by my previous post (about books), I'm
10
by: compiler_newbie | last post by:
Hi, I have the following code that is compiled on gcc422 (linux) with - O3 option. I see that one of the branch is discarded, even though it is not a dead branch. Is this a bug or a feature? ...
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.