Home > Back-end >  C recursive call fails
C recursive call fails

Time:09-18

Int fact (int n, int num)
{
If (n & lt;=0)
return num;
Return fact (n - 1, n + num);
}

Int main ()
{
Int c=1;
C=fact (10000, 0);
STD: : cout & lt; }

After 0 on x00992389 (located in the study. Exe) caused by abnormal: 0 xc00000fd: Stack overflow (x00882f94 x00000001 parameters: 0, 0),
Why the tail call stack overflow will

CodePudding user response:

Whenever calls the function, before this function will be the code before address parameters such as (that is, call point) into the stack, such as the called function will address a stack, program according to the data return calls, if the number of recursive calls too much, will not only into the stack stack, then the stack is destroyed, pressure,,




-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- a delicate small tail -- -- --
https://bbs.gnsoft.ltd
-------------------------

CodePudding user response:

reference 1st floor qq_27458705 response:
whenever calls the function, before this function will be before the code (call), such as address parameters into the stack, such as the called function will address a stack, program according to the data return calls the point, if the number of recursive calls too much, will not only into the stack stack, then the stack is destroyed, pressure,,




-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- a delicate small tail -- -- --
https://bbs.gnsoft.ltd
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Ordinary recursive does, but this is the end call, will not create a new stack, why would burst stack

CodePudding user response:

Other languages may be on the tail recursion (such as JAVA) is optimized, compile-time tail recursion optimization for the while loop, at this time there is no additional stack overhead,

But as far as I know, in the c + + tail recursion compared with ordinary recursive tail recursion is more efficient, less overhead and tail recursion compared with ordinary recursive exist only function stack overhead, but it's not,,,,,,,,




-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- a delicate small tail -- -- --
https://bbs.gnsoft.ltd
-------------------------

CodePudding user response:

Can advice without recursive need not recursive, can use tail recursion is no ordinary recursive




-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- a delicate small tail -- -- --
https://bbs.gnsoft.ltd
-------------------------
  • Related