My concern is that it will use the stack memory in an instruction involving arithmetic and calling a recursive function, for example:
return 5 + f(a-1); //f is recursive
I would like to understand what is pushed onto the stack, and when, so that I know what may or may not cause memory problems in the case of deep recursion. Here is a more complete example:
int f(int a) {
if (a > 0) {
return 5 + f(a-1);
}
else {
return 0;
}
}
Focus on the line. return 5 + f(a-1);What do we need to store in memory around this point?
- We have a stack space for an integer a, since the variable is local to f
- Will values 5 and 1 be saved?
- What about the result of operation a-1, will it be pushed onto the stack?
- what about the return value of f?
" " , - . , , .
? ?
,