我知道,局部变量(以及其他内容)被放置在激活记录中。并且在函数开始执行之前,激活记录必须存在。考虑以下功能:
void f(void)
{
int i;
scanf("%d", &i);
if (i > 10) {
int j = 22;
// do some operations on j here.
}
// more code below...
}
Run Code Online (Sandbox Code Playgroud)
查看此函数,似乎该变量j可能存在或可能不存在,完全取决于运行时用户的输入。在这种情况下,
j将被放置在激活记录中吗?j在if`块之外和之上声明的代码)吗?j如果需要,将在执行期间将其简单地分配到堆栈段上吗?但是,在这种情况下,如何j在if块之后超出范围?我在C11规范中找不到关于此的太多信息。提前致谢。
根据我们的书,每个函数在C中的运行时堆栈中都有一个激活记录.每个激活记录都有一个返回地址,动态链接和返回值.主要还有这些吗?
前三个问题分别得到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 …Run Code Online (Sandbox Code Playgroud)