Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2. Problems With Pointers and New / Delete (TCOs 2, 4, 5, and 6) The following code creates a pointer to an array of ints

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.

image text in transcribed

Figure 1: Memory Leak Output

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

image text in transcribed

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

int* nArray = new int[arraySize];

cout After creating and allocating memory for nArray."

cout and contains the value "

for (int i = 0; i

{

nArray[i] = i*i;

}

cout After initializing nArray."

cout and contains the value "

for (int i = 0; i

{

cout "

}

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

cout Before reallocating memory for nArray."

cout and contains the value "

nArray = new int[arraySize + 2];

cout After reallocating memory for nArray."

cout and contains the value "

for (int i = 0; i

{

nArray[i] = i*i;

}

cout After reinitializing nArray."

cout and contains the value "

for (int i = 0; i

{

cout "

}

// . . . and also here.

cout Getting ready to close down the program."

cout and contains the value "

// 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions