每次调用函数时,激活记录的堆栈(通常只称为堆栈)会新增一条记录。相反地,当函数返回时,它的记录不再使用,因此依此类推。 堆栈(也称为调用堆栈)是一种数据结构,其按照“先进先出”的规则从一端增长和缩小。最后一行是否正确?我在《C++程序设计语言》一书中读到了这句话。
f1()
、f1.1()
、f1.2()
、f1.1.1()
、f1.2.1()
和f1.2.2()
,并且您有以下代码片段:int f1(){
if (<condition>){ // B1
return f1.1();
} else {
return f1.2();
}
}
int f1.1(){
int temp = -1; // B2
return f1.1.1();
}
int f1.2(){
if <other_condition>{
return f1.2.1(); // B3
} else {
return f1.2.2();
}
}
int f1.1.1(){
int temp = 1001001;
return temp;
}
int f1.2.1(){
int temp = 1002001;
return temp;
}
int f1.2.2(){
int temp = 1002002; // B4
return temp;
}
B1-B4表示在那一行设置了断点,执行时会命中这些断点。让我们看看此时调用栈的样子:
B1:调用栈:
f1()
f1.1()
f1()
f1.2() // at the moment of the breakpoint, f1.2.1() not yet executed.
f1()
f1.2.2()
f1.2()
f1()
f1()
(该函数正在执行),当子函数(f1.1()
或f1.2()
)被执行时,该函数将被添加到调用栈的顶部。一旦执行完成,它就会被删除。
stack
- (1) 内存用于保存函数的局部变量。(2) 标准库的先进后出序列。TC++PL 10.4.3、17.3.1,D&E 2.3、3.9." - Tony Delroy