473,382 Members | 1,445 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,382 software developers and data experts.

quarry: Infinite loop without using for , while, do-while

I am sorry for asking this silly question, but i don't understand why
it is happening please suggest me
=================================
#include <stdio.h>
int main()
{
static int i=1;
printf("function call: %d time \n", i);
i++;
main();
}
=================================
this is a infinite loop, I ran this program 5-6 times , This program
gives segmentation fault (core dump) while main() function call at
523477, 523486, 523791... times. why this program give segmentation
fault after calling main() this much of time... what happens when the
main() calls at the last time before SF(CD).
Thanks .
Jun 27 '08 #1
5 1922
Tinku said:
I am sorry for asking this silly question, but i don't understand why
it is happening please suggest me
=================================
#include <stdio.h>
int main()
{
static int i=1;
printf("function call: %d time \n", i);
i++;
main();
}
=================================
this is a infinite loop,
It's infinite recursion.

I ran this program 5-6 times , This program
gives segmentation fault (core dump) while main() function call at
523477, 523486, 523791... times. why this program give segmentation
fault after calling main() this much of time... what happens when the
main() calls at the last time before SF(CD).
What you are asking the computer to do is provide infinite capacity for
recursion. The computer can't do that because it must, at each stage,
remember where it came from, so to speak, and that cannot be done in zero
memory. Even if each recursive call only incurred a single byte (or even a
single bit) of overhead, and even if the computer has all the memory in
the universe, eventually it will exceed its capacity. Infinity is pretty
big.

Your computer responds by giving up.

To fix the problem, provide a base case for the recursion and make sure
that you reach the base case before your machine's resources expire.
Here's a trivial example that would be far better expressed as a simple
loop:

#include <stdio.h>

int main(int argc, char **argv)
{
if(argc 0)
{
printf("%s\n", argv[argc - 1]);
main(argc - 1, argv + 1);
}
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
On 9 Jun 2008 at 10:34, Tinku wrote:
#include <stdio.h>
int main()
{
static int i=1;
printf("function call: %d time \n", i);
i++;
main();
}

this is a infinite loop, I ran this program 5-6 times , This program
gives segmentation fault (core dump) while main() function call at
523477, 523486, 523791... times. why this program give segmentation
fault after calling main() this much of time... what happens when the
main() calls at the last time before SF(CD).
In C, function calls are implemented using the stack. When your process
starts up, it is given a virtual address space, with the stack at the
highest addresses growing downwards towards the heap (and beyond it the
data and text sections).

Each time a function is called, a "stack frame" (local variables, return
address parameters) gets pushed onto the stack, and the stack pointer is
moved down in memory. If you call a function with a sufficiently high
level of recursion, eventually the stack will break through into
non-writable blocks of memory in your address space, and the OS will
trigger a SIGSEGV, as you've discovered.

Jun 27 '08 #3
Antoninus Twink <no****@nospam.invalidwrote:
On 9 Jun 2008 at 10:34, Tinku wrote:
#include <stdio.h>
int main()
{
static int i=1;
printf("function call: %d time \n", i);
i++;
main();
}

this is a infinite loop, I ran this program 5-6 times , This program
gives segmentation fault (core dump) while main() function call at
523477, 523486, 523791... times. why this program give segmentation
fault after calling main() this much of time... what happens when the
main() calls at the last time before SF(CD).
In C, function calls are implemented using the stack. When your process
starts up, it is given a virtual address space, with the stack at the
highest addresses growing downwards towards the heap (and beyond it the
data and text sections).
Each time a function is called, a "stack frame" (local variables, return
address parameters) gets pushed onto the stack, and the stack pointer is
moved down in memory. If you call a function with a sufficiently high
level of recursion, eventually the stack will break through into
non-writable blocks of memory in your address space, and the OS will
trigger a SIGSEGV, as you've discovered.
If you would write "On my implementation, function calls are..."
or "I guess on your implementation,... " it would be ok. But the
moment you make a sweeping statement like "In C,..." this becomes
simply wrong. A stack is one way to implement it, but it's not
mandated by the C standard. And not all systems use virtual ad-
dress space, e.g. DOS does not. The stack pointer starts at a
high address and the stack grows downward on some systems, on
others it's exactly the other way round (and if the heap is above
or below the stack area or if they are neighbors at all is another
implementation detail that isn't mandated by C). Finally, you don't
need to get a segmentation fault, there's nothing in C that would
forbid an implementation to check the stack pointer for bounds and
abort the program with a nice error message about an attempt of
the program to overflow the stack area.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #4
On Jun 9, 4:20 pm, Chris Dollin <chris.dol...@hp.comwrote:
In this case, it can; it's a tail-call, so the call to main can return
to wherever /this/ main was called from. A little stack-bashing will
do the trick nicely. Noting that the called function is /this very
function/ allows the stack-bashing to be replaced by a branch back
to the start of the function ... result, an infinite loop that doesn't
run out of store.
I don't think that the standard mandates transforming tail recursion
calls to
branching instructions. Its up to the compiler which may or may not
chose to
convert the tail calls.
Jun 27 '08 #5
rahul wrote:
On Jun 9, 4:20 pm, Chris Dollin <chris.dol...@hp.comwrote:
>In this case, it can; it's a tail-call, so the call to main can return
to wherever /this/ main was called from. A little stack-bashing will
do the trick nicely. Noting that the called function is /this very
function/ allows the stack-bashing to be replaced by a branch back
to the start of the function ... result, an infinite loop that doesn't
run out of store.

I don't think that the standard mandates transforming tail recursion
calls to branching instructions.
It doesn't. I didn't suggest it did. (I might wish otherwise, of
course.)
Its up to the compiler which may or may not chose to convert the tail
calls.
I didn't mean to imply otherwise, but I see that

Yes, Virginia, compilers will do this.

in my post was ill-written; it should have been "some compilers will
do this" or "compilers can [are allowed to] do this".

--
"Tells of trouble and warns of change to come." /Lothlorien/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #6

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

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
7
by: Kewlpack | last post by:
Okay - this is stumping me. I've used .Net since 1.0 release and never had this trouble before... In one of my new projects, if I enable the SmartNavigation="True" in the Page...
5
by: Allerdyce.John | last post by:
Hi, I have this piece of code which loops thru a STL list, but that causs an infinite loop. bool Executer::group(MyList& bl, ResultList & grl) { for (ExecuterList::iterator i =...
7
by: Curious | last post by:
Hi, I have a C# class that contains a property defined as follows: public bool bRunTwice { get { return bRunTwice; } set { bRunTwice = value; } }
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.