What is the difference between (1) an infinite loop and a recursive function?

what happens in the operating system? for recursive function mayb stack overflow and for while (1)? please correct me if i am wrong?

+6
operating system
source share
10 answers

A recursive function will call itself repeatedly. An infinite loop will continue to repeat the same code. Although this may seem very similar, the actual effects are very different. Each time a method is called, variables are pushed onto the stack. Of course, this means that there are inherent limits on the number of times a function can recurs. Therefore, while your infinite loop runs forever, in practice, the recursive function will end up from the stack space, and the application will most likely stop.

+14
source share

A recursive function calls itself, which pushes the parameters onto the stack. This can go on forever, which ultimately leads to a stack overflow. Some compilers can optimize this by essentially turning a recursive function into a while loop - this is called tail recursion.

The while loop just goes back to the top and uses the same space again, so it can work literally forever (at least until the power goes out :-))

+8
source share

A recursive function continues to call itself, while an infinite loop continues to repeat the same block of code.

When a function is called, some storage must be reserved to store the return value on the function call stack . Thus, with sufficiently recursive function calls, the stack space will be exhausted and cause a stack overflow.

Consider

#include <stdlib.h> #include <stdio.h> int somefunc(int x) { printf("%d\n", x); return somefunc(rand()); } int main(void) { return somefunc(0); } 

The program described above will eventually terminate or cause serious damage, unlike

 int somefunc(int x) { return printf("%d\n", x); } int main(void) { while ( 1 ) { somefunc(rand()); } return 0; } 

which will work until the user stops working (by pressing CTRL-C or turning off the computer.)

+3
source share

A recursive function may end depending on how it is encoded. Of course, there is no need to end the stack overflow. The while(1) can also end if it has gaps or returns.

+3
source share

A recursive function has a sentence in which it does not call itself, that is, it ends.

+1
source share

In an infinite while (1) loop, the stack space for the stack frame will be allocated (information about where to return if / when the function returns), and any local variables declared in the same function once in the stack will be allocated, regardless of how many iterations are performed by the loop.

Therefore, for 1,000,000 iterations, the stack will be sizeof (stack frame) + sizeof (any local variables)

so if the stack stack is 16 bytes and int is 4 bytes, the function will allocate 20 bytes on the stack.

While a recursive function allocates space on the stack for the stack frame (information about the function being returned) and any local variables every time the function calls itself.

Therefore, for 1,000,000 iterations, (sizeof (frame frame) + sizeof (any local variables)) * 100,000

so (using previous sizes) 20bytes * 1,000,000 == 20,000,000bytes == (approximately) 19MB

+1
source share
Function

returns push (the place in memory where the next recursive function was called from) the address on the stack and goes to the place in memory (the recursive function). And although you do not need to store the ret address, because it only jumps at the beginning.

0
source share

When a function is called, some memory is allocated for it on the stack. It supports: - parameter values โ€‹โ€‹passed to the function - return address - the address in memory where execution will go after the function returns - local variables - other things that depend on the implementation

Thus, each function call in a recursive function allocates memory. Therefore, you must be careful during design.

In the case of while (1), a process that runs a program with an infinite loop, for example while (1) {;} simply repeats part of the commands. Keeping such a process operational in an operating system that simply takes up processor time and some memory.

0
source share

For simple code and a good compiler, there is no difference, but for a naive compiler with a non-recursive recursive function, you will end up with a stack space called stack overflow.

As for the questions: in the stack overflow: the stack is mapped to a location or memory (for each process) that has unallocated memory next to it. The stack is used to store the values โ€‹โ€‹of local functions, the return address of the current function is also placed on the stack, and then the values โ€‹โ€‹are transferred to the stack. Thus, the stack consumption for each function call. Therefore, when the processor tries to access memory, skip the end of the allocation space, thus causing a memory access exception and is interpreted as a stack overflow.

For some time (1), the behavior of the operating system depends on what kind of multitasking behavior you are using the OS. For a proactive system (such as Window NT and newer), it just sees that your process has a lot of work, but if it has a user interface and you do not respond to the windows you send, you will receive a message "This application seems to have stopped respond".

Somewhere, as if you had an OS other than proactive, then it will hang if you do not remove control over the OS, so in Windows 3.1 days the printer driver will freeze the entire system during printing. Well, the HP drivers did.

In an embedded system, in order to avoid software blocking, they usually have a hardware timer called a watchdog timer, which, if not tickled every second, will reboot the system. Thus, preventing the system from staying in a locked state.

0
source share

while (1) does not create a new evaluation context, recursion (if not optimized by the compiler). This means a few things:

  • In the entire practical implementation, recursion will exceed the limit when a new evaluation context cannot be created (i.e., a stack overflow occurs).
  • A new evaluation context means new copies of any local variables that you declare.
  • It is trivial to exit while (1) with a simple return , but return only exits the current context in recursion. It is not easy to completely get out of multiple nested contexts.
0
source share

All Articles