nurv
nurv

Reputation: 53

What is difference between an infinite loop and an infinite recursive call?

What is the difference between the following two codes?

int main()
{
    printf("hello\n");
    main();
}

and

int main()
{
    while(1)
        printf("hello\n");
    return 0;
}

The first one is pushed on stack every time while the stack allocation for second is done only once. Is there anything related to memory overflow in the while infinite loop?

Upvotes: 1

Views: 3410

Answers (7)

Some programmer dude
Some programmer dude

Reputation: 409166

You should note that the C specification doesn't actually mention anything about "the stack". Using the stack is just an implementation detail, and compiler-designers/-programmers are free to use any other method to implement function calls and local variable storage. However, which method is used, it ultimately will use up some resource (like memory) and like everything else the resources on a computer are limited, so using them up will sooner or later cause some kind of overflow situation.

On the other hand, an empty while loop doesn't actually use any extra resources no matter how many iterations it does. In your case it's more complicated than that because you don't know what printf does or what resources it uses or what resources the layers beneath the printf call uses, so there might be a limit there too. For example, what if the terminal window is set to use "infinite" scrollback, then that scrollback will expand until it consumes all available memory.

Upvotes: 1

bytecode77
bytecode77

Reputation: 14820

The loop will run forever, while the recursive code will eventually cause a stack overflow as the stack buffer runs full.

If you have the choice between a recursive function and an iterative, you should prefer the iterative, as recursive code can lead to stack overflow and other issues more likely. Also, it consumes memory (on the stack), while a loop doesn't allocate more memory each run.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96258

Infinite recursion is always a horrible idea. Don't do that.

You are at the mercy of your compiler. If it optimizes it into an infinite loop, you're lucky. If not, you'll run out of stack space and your app will (most likely) crash.

Upvotes: 0

shauryachats
shauryachats

Reputation: 10385

Infinite recursion will cause an ugly Segmentation Fault, second one will run forever.

Upvotes: 0

Hossein Atashi
Hossein Atashi

Reputation: 41

The main obvious difference is that the first example will certainly result in a stack overflow while the second example does not.

That's due to the fact that in the first case every time main() is called the return address is pushed to the stack before jumping to the start of the main() function. These return addresses are never popped from the stack since the function never returns.

In the infinite loop, this is not the case and only the printf() function is called and returned from over and over again which will not result in a change in the stack pointer.

Upvotes: 4

Codor
Codor

Reputation: 17605

The first one will most likely generate a stack overflow while the second one will potentially run forever.

Upvotes: 2

Brian Agnew
Brian Agnew

Reputation: 272237

As noted, the recursive method will consume stack, and ultimately you'll run out of stack space.

Upvotes: 5

Related Questions