displaying activation records

This commit is contained in:
Jidong Xiao
2023-09-19 23:23:14 -04:00
parent a510e367eb
commit 538ec774a7

View File

@@ -145,9 +145,7 @@ for (int j=i; j<n; ++j)
![alt text](factorial_equation.png "factorial equation") ![alt text](factorial_equation.png "factorial equation")
- Computing integer powers is defined as: - Computing integer powers is defined as:
<div style="margin-top: 10px;">
![alt text](power_equation.png "power equation") ![alt text](power_equation.png "power equation")
</div>
These are both examples of recursive definitions. These are both examples of recursive definitions.
@@ -188,6 +186,7 @@ confusing since there are multiple activation records waiting for an answer from
- This is illustrated in the following diagram of the call fact(4). Each box is an activation record, the solid lines - This is illustrated in the following diagram of the call fact(4). Each box is an activation record, the solid lines
indicate the function calls, and the dashed lines indicate the returns. Inside of each box we list the parameters indicate the function calls, and the dashed lines indicate the returns. Inside of each box we list the parameters
and local variables and make notes about the computation. and local variables and make notes about the computation.
![alt_text](activation_records.png "chain of activation records")
- This chain of activation records is stored in a special part of program memory called the stack - This chain of activation records is stored in a special part of program memory called the stack
## 7.13 Iteration vs. Recursion ## 7.13 Iteration vs. Recursion
@@ -197,10 +196,10 @@ version of factorial:
```cpp ```cpp
int ifact(int n) { int ifact(int n) {
int result = 1; int result = 1;
for (int i=1; i<=n; ++i) for (int i=1; i<=n; ++i)
result = result * i; result = result * i;
return result; return result;
} }
``` ```
@@ -242,25 +241,25 @@ recursive function. It is common to have a driver function that just initializes
```cpp ```cpp
void print_vec(std::vector<int>& v, unsigned int i) { void print_vec(std::vector<int>& v, unsigned int i) {
if (i < v.size()) { if (i < v.size()) {
cout << i << ": " << v[i] << endl; cout << i << ": " << v[i] << endl;
print_vec(v, i+1); print_vec(v, i+1);
} }
} }
``` ```
```cpp ```cpp
void print_vec(std::vector<int>& v) { void print_vec(std::vector<int>& v) {
print_vec(v, 0); print_vec(v, 0);
} }
``` ```
What will this print when called in the following code? What will this print when called in the following code?
```cpp ```cpp
int main() { int main() {
std::vector<int> a; std::vector<int> a;
a.push_back(3); a.push_back(5); a.push_back(11); a.push_back(17); a.push_back(3); a.push_back(5); a.push_back(11); a.push_back(17);
print_vec(a); print_vec(a);
} }
``` ```
How can you change the second print vec function as little as possible so that this code prints the contents How can you change the second print vec function as little as possible so that this code prints the contents