【发布时间】:2011-01-03 15:25:01
【问题描述】:
前 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
【问题讨论】:
-
如果这是一个家庭作业问题(很明显考虑到您的其他问题),至少有礼貌用标签这样说
-
第一个问题是什么?
-
如果这是你的导师给你的课程,你应该考虑改变你所在的班级,或者更换导师。
-
如果不知道更多正在使用的“规则”,这是一个不可能回答的问题。什么是“位置”,每个 int、指针、返回地址等使用了多少个“位置”?可以/将在寄存器中传递多少个(如果有)什么类型的参数?编译器会做像内联和/或死代码消除这样的优化吗?消除死代码后,答案很可能为零(即它是全部死代码)。
-
天啊,看到 CS 课上发生的事情真是太可怕了。在许多处理器上,您可以想象 sum() 不使用堆栈,只使用寄存器。在其他人相当多的堆栈。但主要是 - 谁在乎答案是 4,5 还是 6。而且可以肯定的是,任何体面的编译器都不会为 fred() 或 barney() 使用堆栈
标签: c activation-record