Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 8pt Consider the following class instances in a C++ program 1 static m

ID: 3596864 • 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(); foo) 6 delete A; 7 return 0 10 void foo ) 2 myClass C; 13 Anew myClassO 14 a 3pt) What is the storage allocation (static/stack/heap) for the myClass objects associated with A, B, C? b) Spt) 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 6 7 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 represen the lifetime). Assume the lifetime of a heap-allocated object starts from new and ends after delete.

Explanation / Answer

Before answering the question let's go through some terms :

HEAP : Heap is a bunch of memory that can be used dynamically (which can be done using new , malloc). It's lifetime is defined by the user using new and delete.
STACK : Names local to a function are allocated space on a stack
STATIC : Global constants and other data generated by the compiler are allocated static storage.


Solution :

1.So looking at the definitions above we can easily determine the storage allocation for A,B,C :

A -> static when "static myClass* A" , heap when new used "A = new myClass()"
B -> heap (memory allocated dynamically using new)
C -> stack (as it has been declared inside a function without new)

2. Now let's look at the life time of A,B,C
A -> 13 - 6 (delete used)
B -> 4 - till end (delete not used)
C -> 12 - 13 (within the function)

**If you have any query , please feel free to comment with details.
**Happy leaning :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote