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



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
.



Relevant Pages

  • Re: Raatikainens critique of Chaitin
    ... dealing with exponential Diophantine equations was ... A subset S of N is bi-immune for a class C if S is infinite but does ... You couldn't have a bi-immunity result for the problem of whether ... Bi-immunity was a concept from the early years of recursion theory, ...
    (sci.math)
  • slightly OT: Sudoku solver C source
    ... takes 162 bytes per recursion level, times a maximum of 81 recursion ... void setbox(int n, int x, int bit) ... to indicate an unused square. ... program to restore the bitmask from a previously saved value. ...
    (comp.emulators.apple2)
  • Re: Halting Problem
    ... computers (infinite time and infinite memory). ... indicating that a program x contains no indefinite loop or recursion. ...
    (comp.programming)
  • Re: new java programmer with compile error and trouble writting a Recursive Function
    ... public static int handshakes ... When a new person enters the room, does his handshake count really multiply by the previous handshake count? ... Which operation is appropriate for "op" in the recursive function? ... Recursion is a fundamental concept of maths and computer science. ...
    (comp.lang.java.programmer)
  • Re: StackOverflowException with attribute
    ... I figured that the recursion wasn't infinite. ... it would seem that the call to GetProperties on the ... You need to find some way to break this loop. ...
    (microsoft.public.dotnet.languages.csharp)