前 3 题我分别得到 6、4、3,但我不知道最后一题怎么算。但是,解决方案手册将 7、5、4、18 作为答案。
int sum(int x[], int N) {
int k = 0;
int s = 0;
while (k < N) {
s = s + x[k];
k = k + 1;
}
return s; // the activation record for sum will be ____________ locations
}
int fred(int a, int b) {
return a + b; // (2) the activation record for fred will be ____________ locations
}
void barney(int x) {
x = fred(x, x);//(2) the activation record for barney will be ____________ locations
}
void main(void) {
int a[4];
int x = sum(a, 4);
barney(x);
} // (3) the stack must have at least _____________ locations to run this program
最佳答案
我不知道你的书的约定是什么,但我假设,返回地址、返回值地址和中间结果的地址总是有的
a)返回地址,返回结果地址,x,N,k,s,中间结果为s + x[k] = total 7
b)ret。 addr,返回结果的地址,a,b,int。水库a+b = 总计 5
c)ret。 addr,ret结果的addr,x,fred返回结果的空间=total 4
d) 最后一个不是在任何给定点询问激活记录所需的最大堆栈大小。它调用 sum,它调用 barney,barney 调用 fred,即 7+5+4 = 16。并且 16 + a + x = 总共 18 个位置来运行这个程序。
请注意,此计算是基于我对您的书籍惯例的随意猜测。
关于c - 激活记录长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2039979/