Pointer & Stack WS

For each problem fill out the "stack" table based on the given code and determine what is printed. Assume each variable takes 4 bytes of memory. Assume space is allocated for all parameters and variables as you enter each function – allocate them in the order they are listed. When a variable goes out of scope, cross out its name but leave its value behind.

1.  / int main() {
int i = 20;
int* p = &i;
cout < i;
} / Stack:

Output:
2.  / int main() {
int i = 20;
int* p = &i;
if (true) {
int j = 5;
p = &j;
}
cout < *p;
} / Stack:

Output:
3.  / int main() {
int i = 20;
int* p1 = &i;
int* p2 = p1;
*p2 = 5;
cout < i < " " < p1;
} / Stack:

Output:
4.  / int main() {
int i = 20;
int j = 5;
int* k = &i;
int** m = &k;
**m = 50;
k = &j;
cout < i;
} / Stack:

Output:
5.  / int main() {
int i = 20;
int j = 100;
int* k = &j;
int* m = k;
*m = 50;
int* p = &i;
int** n = &k;
**n = 300;
*n = p;
cout < j;
} / Stack:

Output:
6.  / void foo(int a) {
a = 10;
}
int main() {
int i = 20;
foo(i);
cout < i;
} / Stack:

Output:
7.  / void foo(int* a) {
*a = 10;
}
int main() {
int i = 20;
foo(&i);
cout < i;
} / Stack:

Output:
8.  / void bar() {
int y;
cout < y; //not a wise move…
}
void foo() {
int x = 10;
}
int main() {
int i = 20;
foo();
int j = 5;
bar();
} / Stack:

Output:
9.  / void foo(int* a) {
int x = 10;
int* p = a;
p = &x;
}
int main() {
int i = 20;
foo(&i);
cout < i;
} / Stack:

Output:
10.  / void bar(int b) {
return;
}
int* foo(int a) {
return &a;
}
int main() {
int i = 20;
int* p = foo(i);
bar(15);
cout < *p;
} / Stack:

Output: