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

2. Problems With Pointers and New / Delete (TCOs 2, 4, 5, and 6) The following c

ID: 3853014 • Letter: 2

Question

2. Problems With Pointers and New/Delete (TCOs 2, 4, 5, and 6)

The following code creates a pointer to an array of ints and allocates space for five ints. It then loads the array with the square of values 0 through 4 and displays the results. Because this is a dynamic array allocation, we can reallocate a larger array. This time we allocate space for seven ints and load and display the squares. In the process, our code has created a memory leak, because we failed to follow the hard and fast rule of for every new there must be a delete. When you run this program (before you fix the memory leak), the output should look something like the following.

Figure 1: Memory Leak Output

Here’s a printout of what the values should be. The actual addresses, of course, will be different, but they should follow the same pattern.

Figure 2: Desired Output

Notice in Figure 1 that the address of nArray never changes and is the same for nArray[0] in both loops.

For this assignment, fix the memory leak. The output should look similar to Figure 2.

Here is the code.

// Week 2 Assignment-2

// Description: Problems with pointers and new/delete

//----------------------------------

//**begin #include files************

#include // provides access to cin and cout

//--end of #include files-----------

//----------------------------------

using namespace std;

//----------------------------------

//**begin global constants**********

const int arraySize = 5;

//--end of global constants---------

//----------------------------------

//**begin main program**************

int main()

{

       cout << endl << endl;

       int* nArray = new int[arraySize];

       cout << " --->After creating and allocating memory for nArray." << endl;

       cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;

       for (int i = 0; i < arraySize; i++)

       {

              nArray[i] = i*i;

       }

       cout << " --->After initializing nArray." << endl;

       cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;

       for (int i = 0; i < arraySize; i++)

       {

              cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray+i << ">" << endl;

       }

       // You'll need a command here to fix the memory leak

       cout << endl << " --->Before reallocating memory for nArray." << endl;

       cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << endl;

       nArray = new int[arraySize + 2];

       cout << dec << " --->After reallocating memory for nArray." << endl;

       cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;

       for (int i = 0; i < arraySize+2; i++)

       {

              nArray[i] = i*i;

       }

       cout << endl << " --->After reinitializing nArray." << endl;

       cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;

       for (int i = 0; i < arraySize+2; i++)

       {

              cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray+i << ">" << endl;

       }

       // . . . and also here.

       cout << endl << " --->Getting ready to close down the program." << endl;

       cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;

       // Wait for user input to close program when debugging.

       cin.get();

       return 0;

}

//--end of main program-------------

//----------------------------------

After creating and allocating memory for nArray. Array address is 4011C6 E30> and contains the value o dodcdod After initializing nArray Array address is Before reallocating memory for ATArray. Array address is 4011C6E30 and ontains the value 0 After reallocating memor or Array Array address is 4011C6 6E8 and contains the value cdcdcdcd After reinitializing nArray Array address is 4011C66E8 and contains the value Array CO1 0 at address 4011 C66E8 Array[11 1 at address 4011 C66EC Array[21 4 at address 4011 C66F0 nArray[3 1 9 at address 4011 C66F4 Array D41 16 at address 4011 C66F8 Array[51 25 at address 4011 C66FC n Array[61 36 at address 4011C6700 >Getting ready to close down the program Array address is 4011C66E8 and contains the value 0

Explanation / Answer

To fix the memory leaks,e eed to deallocate the array before every reinitialisin and at the end delete the arraypointer.

#include<iostream>
using namespace std;
#define arraySize 5
int main()
{
cout << endl << endl;
int* nArray = new int[arraySize];
cout << " --->After creating and allocating memory for nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
for (int i = 0; i < arraySize; i++)
{
nArray[i] = i*i;
}
cout << " --->After initializing nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
for (int i = 0; i < arraySize; i++)
{
cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray+i << ">" << endl;
}
// You'll need a command here to fix the memory leak
cout << endl << " --->Before reallocating memory for nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << endl;
delete [] nArray; // the [] is needed when deleting array pointers
nArray = new int[arraySize];
cout << dec << " --->After reallocating memory for nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
for (int i = 0; i < arraySize+2; i++)
{
nArray[i] = i*i;
}
cout << endl << " --->After reinitializing nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
for (int i = 0; i < arraySize+2; i++)
{
cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray+i << ">" << endl;
}
   delete [] nArray;
   nArray = 0;
// . . . and also here.
cout << endl << " --->Getting ready to close down the program." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
// Wait for user input to close program when debugging.
cin.get();
return 0;
}

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