Limited recursion in C?

I launched this program and displayed

...

65088
65089
65090

and then he stopped. Windows 7 reported that a.exe stopped working. Here is the code:

#include <stdio.h> void go(void); main() { go(); } void go(void) { static int i = 0; printf("%d\n", i++); go(); } 

I think this program should constantly print numbers due to recursion, but it stops at 65090! C code compiled with gcc. Any ideas?

+6
source share
9 answers

At some point, you are overflowing the stack, because every call to go() must push the return address onto the stack, even if you do not pass the arguments of the function to the function call. Thus, every call to go() occupies a block of size pointer on the stack for this return address. Since the stack has a limited size, this means that at some point you will run out of free space. The C language does not indicate that tail-call optimization should occur under such circumstances, although some compilers (e.g. gcc) offer this option using optimization switches. But it will be something specific to the compiler and independent of the language specification.

+14
source

It stops because it overflows the stack. Following the results are undefined.

+10
source

You cannot chat endlessly on a machine with limited resources - in particular, the stack. Each subroutine call required more stack space, and you will end up.

+7
source

By "stopped working" do you mean crashed? That would make sense. Each program has a limited stack depth, and each recursion level (i.e., each new function is called) is pushed onto the stack. Therefore, if your program reaches its maximum stack depth, it will work!

+2
source

It seems your compiler is not performing tail call optimization . Despite the lack of reasons why the run-time system supports stop calls, it does so in any way.

+2
source

The recursion depth is limited by the size of the program stack. Each call pushes additional information onto the stack, and when the stack is exhausted, it overflows. In your case, you received 65090 frames on the stack before overflow, after overflow - your program is dead.

+1
source

I think your stack has run out of free space. Only a certain amount of memory is allowed in your program (which can change your environment dev), and since the go () call is called internally, 65,000 copies of the function call appear simultaneously.

+1
source

First of all, it is not C, which limits recursion . This is your operating system!

This is because recursion uses the stack. Do you know what a stack is ? This is the application’s memory area that is used to store (local) variables or parameters that need to be sent from one function to another during a function call. It also stores other information, such as the address of the function, to which it should return after executing the current one.

Now you see that infinite recursion is a problem because after each recursion more data is stored on the stack, and since these functions are never returned, their resources on the stack are also never freed. At some point, the stack will run out of memory and it will explode in your face.

On Windows, if you need to increase the size of the stack, you can try this .

+1
source

Regardless of whether it will be recursion or not, once you reach INTMAX , your program can do what it likes, since int overflow is undefined behavior. And since static analysis can show this, your whole program can do what you like.

0
source

Source: https://habr.com/ru/post/891318/


All Articles