To be done in C Project Please answer the following question

To be done in C++

Project:

Please answer the following questions by send me your answers in a Word or Notepad document that you include with your .zip file submitted for this unit.

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 ]; _____________________________

Solution

1)

2)

sample code

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

solutions

pl should not be deleted

pj should not be deleted

here newInt and pInt can be deleted because those variables does not serve any purpose.Along with those variables entire line also should be deleted

3) sample code

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;
}

comment

There are lot of errors in the code which you hae posted.you did not do the declaration for most of the variables.

so output will not be generated.Even the declarations done in some places are incorrect.for stars and pointers you must use some variable which keep the count and those are also not mentioned

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;
To be done in C++ Project: Please answer the following questions by send me your answers in a Word or Notepad document that you include with your .zip file subm
To be done in C++ Project: Please answer the following questions by send me your answers in a Word or Notepad document that you include with your .zip file subm

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site