Problem 1 [8pt] Consider the following class instances in a C++ program: 1 stati
ID: 3596959 • Letter: P
Question
Problem 1 [8pt] Consider the following class instances in a C++ program: 1 static myClass A; 2 int main () myClass* B = new myClass(); 5 foo ) delete A; return 0; 10 void foo () 12 13 14 myClass C; A = new myClass(); a) (3pt) What is the storage allocation (static/stack/heap) for the myClass objects associated with A, B, C? b) (5pt) Consider one execution of the program above. The execution trace (a sequence of program state- ments executed at run time) of this program is 4 5 12 13 67 For each myClass object associated with A, B and C, as well as each pointer stored in A and B, write down their lifetimes (use a subset of execution trace, e.g., 12 13 to represent the lifetime). Assume the lifetime of a heap-allocated object starts from new and ends after delete.Explanation / Answer
a)
Object A will have static allocation as it has been declared as static.
Objects B and C will have dynamic or Heap allocation as new operator always allocates memory dynamically
b)
life time:
A {4, 5, 12, 13, 6, 7}
B {4, 5, 12, 13, 6}
C {12, 13}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.