Assumed infinite recursion, non-return functions

Infinite recursion is most often not needed, and when this happens, it usually causes stack overflows or segfaults.

But for the sake of theory and simple curiosity, I was wondering if it was possible to create real infinite recursion intentionally.

Work in C ++ and C, where the stack, as a rule, grows for each function call, and each function returns and returns a part of the processed stack.

Here is a thought. Is it possible to make a function clear its own stack space, and then call another function so that the new function effectively replaces the first function without the first function, which should have been returned, and then started again through a loop.

I do not just think of simple loops as a possible use for this if they were. Loops usually do a good job for what they do. But what if you use it to send signals through the node network that carry indefinite time in their process threads until they reach a certain state. It can be a tool that can be used for some problems.

Remember, I really do not ask how practical it is, only if it is possible. For science!

+7
source share
3 answers

Is it possible to make a function clear its own stack and then call another function so that the new function effectively replaces the first function

This is used for tail-call-optimization , so yes, it is possible and used in practice. In languages ​​like C ++, this is a nice feature because sometimes algorithms are easier to express with recursion, but are more efficiently implemented using a loop. Conversion in some cases may be performed automatically by the compiler.

+15
source

There is a method called trampolining that is used to implement programming to continue the run without using tail call optimization. If you consider any language without TCO support, such as JavaScript, and research solutions for CPS in that language, then it is likely that the solution includes a springboard.

In essence, with a trampoline there is a function called a trampoline that iteratively calls the function return function.

I know that you said "without the first function that should be returned, and then run again through the loop" - what is a trampoline - but considering that it is C ++, leaving areas, for example, returning is central to the core design of automatic resource management C ++ through destructors (see: RAII). Otherwise, you can use the C functions setjmp() / longjmp() to destroy the stack, but then you need to be very careful to make sure all resources are released properly.

+5
source

This reminds me of the optimization that can be done in assembler code. Say you have this:

  call FuncA call FuncB 

you can replace it:

  push ReturnAddress push FuncB jmp FuncA ReturnAddress: 

This causes ret at the end of FuncA go directly to FuncB and not back to the caller and then to FuncB . Not really possible in higher level languages.

There also:

  call FuncA ret 

which can be changed to:

  jmp FuncA 
+2
source

All Articles