How much memory will the following program have allocated? Memory Management. As
ID: 3581917 • Letter: H
Question
How much memory will the following program have allocated?
Memory Management. Assume that variables of type int or float each consume 4 bytes of memory and that each pointer also consumes 4 bytes of memory. How much memory space (in bytes) will the following program have allocated - whether on the stack or dynamically, and whether leaked or not - by the time we reach Point A of the program, and how much memory space will have leaked by the time the program reaches point A: class A {int numltems; int* items; A(int n) {numltems = n; items = new int[numltems];}}; int main() {A* firstObject; for(int i=1; iExplanation / Answer
In main
first statement is pointer to object = 4 bytes for pointer
in for loop object created
for iteration i = 1, pointer points to object of class with 1 array elemnt + one integer numItem + items pointer in class
4 bytes for items pointer + 4 bytes for integer variable numItem + 4 bytes for one array element = 12 bytes
for iteration i = 2 ,
4 bytes for items pointer + 4 bytes for integer variable numItem +8 bytes as 2 array items(4bytes*numberof items) = 16 bytes
for iteration i = 3;
4 bytes for items pointer + 4 bytes for integer variable numItem +12 bytes as 3 array items(4bytes*numberof items) = 20 bytes
When we reach point A: total memory allocated
object pointer + memory allocated in for loop for each object = 4 + 12 +16 +20 = 52 bytes.
Leaked memory by the time we reach point A:
only the pointer variables allocated using new becomes leaked memory as other memories go out of scope after main exists.Hence 4 bytes + 8 bytes+ 12 bytes for array elements = 24 bytes are leaked memory
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.