Consider the following class instances in a C program static
Consider the following class instances in a C++ program: static myClass A; int main() {myClass* B = new myClass (); foo(); delete B; return 0;} void foo() {myClass* C = new myClass(); myClass D;} What is the storage allocation (static/stack/heap) for the objects associated with A. B. C and D? Consider one execution of the program above. The execution trace, a sequence of program statements executed at run time, of this program is 4 5 12 13 6 7 For each object associated with A. B. C, and D, write down its lifetime (use a subset of execution trace, e.g., 12 13 to represent the lifetime).
Solution
a)
memory associated with
A - static
B - head (dynamically created)
C - heap (dynamically created)
D - stack
b)
life time:
A - 4,5,12,13,6,7
B - 4,5,6,7
C - 12, 13
D - 13
