Question: Allocating Heap Blocks. The function new allocates a block from the heap an delete returns them to the heap. Given the following pictorial view of

Allocating Heap Blocks. The function new allocates a block from the heap an delete returns them to the heap. Given the following pictorial view of the heap, where a block with a numerical value or a dash ('-') in it has been allocated and the block without values are unused. Assume the type int can be stored in one block. (6 points)

|__81___|___-___|__36___|__100__|___49__|__25___|

|_______|_______|__64___|_______|_______|_______|

|_______|__-3___|___-___|___-___|_______|_______|

|_______|_______|_______|_______|_______|_______|

|_______|_______|_______|_______|_______|_______|

a) What would the heap look like after a call to int * p = new int [5]; // allocate 5 blocks of type int p[4]=p[3]=p[2]=p[1]=p[0]= 0; // set them all to zero

b) What would it look like after int *q = new int [2]; // allocate 2 blocks of type int q[0]= 1; q[1]= 2; // set them to values 1 & 2

c) What would it look like after a) and then a call to delete[] p; // delete all five element of p

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!