i*****f 发帖数: 578 | 1 int f1(int n)
{
if (n<0) {
return 0;
}
return f(n-1) + 1;
}
int f2(int n, int sum)
{
if (n<0) {
return sum;
}
return f(n-1, sum+1);
}
if compiled w/o optimization, and run with n=0xffff, f2 gives a stack
overflow. f1 is good.
if compiled w/ optimization, both are ok.
cl.exe on Win7:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for
80x86
Not even see the stack overflow on Ubuntu 7.04+GCC w/o optimization. | b****j 发帖数: 78 | 2 1. each call of f1 uses less stack space than f2
2. default stack size set by cl might be different than gcc
【在 i*****f 的大作中提到】 : int f1(int n) : { : if (n<0) { : return 0; : } : return f(n-1) + 1; : } : int f2(int n, int sum) : { : if (n<0) {
| i*****f 发帖数: 578 | 3 right, just confirmed from the assembly that neither is tail call optimized.
Thanks!
【在 b****j 的大作中提到】 : 1. each call of f1 uses less stack space than f2 : 2. default stack size set by cl might be different than gcc
|
|