30
A question that has everything to do with the name of this site.
We know that one of the most commonly used examples to demonstrate the execution stack of a program is recursion. A recursive function must have: stop condition and recursion must solve a smaller instance of the same problem.
Recursive calls will be stacked and then popped (executed), from the smallest case to the largest, until the final value is returned.
If no stop condition exists or the recursive call is not for a smaller instance, the program goes into loop infinite and occurs Stack Overflow.
Given the code below developed in C:
#include <stdio.h>
#include <conio.h>
int fat(int n);
int main()
{
int n;
printf("Calculo de FATORIAL\n");
printf("Entre com um numero:");
scanf("%d",&n);
printf("Fatorial:%d",fat(n));
getch();
return 0;
}
int fat(int n)
{
if (n==1)
return(1);
return(n * fat(n-1));
}
How would run stack 4!, for example?
If there was no stopping condition, the program would stack up until battery overflow occurs. At what point this would occur, i.e., how large the stack would be?
I don’t know if I understand. I understand the problem. But what would be your doubt? What do you want to know with this "when"?
– Maniero
I edited the question to make it clearer. In short I would like to show the execution stack to 4! in a didactic way and know when it occurs stack overflow, IE, which is the size of this stack.
– Denis Caixeta