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

Please answer the following questions This is to be done in c++ For each of the

ID: 3841591 • Letter: P

Question

Please answer the following questions This is to be done in c++

For each of the dynamic variable declarations listed on the left, provide the necessary delete statement that would destroy this dynamic variable and return the memory it uses to the heap so that other variables may use this memory.

Appropriate Delete Statement

Consider the following code. Which of the variables should be deleted when the program ends?

Variables That Need To Be Deleted

int i = 1, j = 2;
int * pI = &i;
int * pJ = &j;
int * newInt = new int( 12 );
int * pInt = new int( 13 );

Consider the following code. What is the output generated by this program?

Output Generated

d2 = 10.0;
pd1 = new double( 22.5 );
pd2 = &d2;
*pd1 = *pd2;
*pd2 = 33.4;
d2 = 5.0;
cout << *pd1 << endl;
cout << *pd2 << endl;
if (*pd == *pd2) {
   cout << “stars =” << endl;
}
if (pd1 == pd2) {
   cout << “pointers =” << endl;
}

Dynamic Variable Declaration

Appropriate Delete Statement

int *I = new int(12); _____________________________ Student *s = new Student(“howie”); _____________________________ int *array = new int[ size ]; _____________________________ double *darray = new double[ 5 ]; _____________________________

Explanation / Answer

Answer:-

1)

Dynamic variable declaration

Appropriate delete statement

int *I = new int(12);

delete[] l;

Student *s = new Student(“howie”);

delete[] s;

int *array = new int[ size ];

delete[] array;

double *darray = new double[ 5 ];

delete[] darray;

2)

Sample code

Variables that need to be deleted

int * pI = &i;

No

int * pJ = &j;

No

int * newInt = new int( 12 );

Yes

int * pInt = new int( 13 );

Yes

Because newInt and pInt are allocated dynamically so they can only be deleted, while pI and pJ not.

3)

Output for the given code:-

10

33.4

Note:- I don’t see pd anywhere in the program, if possible correct it and if its pd1 then also they are not equal do output will be remain same.

Dynamic variable declaration

Appropriate delete statement

int *I = new int(12);

delete[] l;

Student *s = new Student(“howie”);

delete[] s;

int *array = new int[ size ];

delete[] array;

double *darray = new double[ 5 ];

delete[] darray;