Question: Instructions: Consider the following C + + program. It asks the user for the sizes of two dynamic arrays, and initializes these arrays to contain

Instructions:
Consider the following C++ program. It asks the user for the sizes of two dynamic arrays, and initializes these arrays to contain the values 17 and 42 respectively.
// Include statements
#include
#include
using namespace std;
// Main function
int main()
{
// Get user input
int size1=0;
cout << "Enter size of array1:
";
cin >> size1;
if (size1<0)
size1=0;
// Get user input
int size2=0;
cout << "Enter size of array2:
";
cin >> size2;
if (size2<0)
size2=0;
// Process array1
int * array1= new int[size1];
cout << "array1:
";
for (int i =0; i < size1; i++)
{
array1[i]=17;
cout << array1[i]<<"";
}
cout << endl;
// Location A
// Process array2
int * array2= new int[size2];
cout << "array2:
";
for (int i =0; i < size2; i++)
{
array2[i]=42;
cout << array2[i]<<"";
}
cout << endl;
// Location B
// Location C
return 0 ;
}
Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages. Run the program with a variety of input values to see what it prints. You should see a lot of 17's and 42's printed on the screen.
Step 2: It is very important to return memory to the operating system when you are finished working with it. Otherwise, your program will have a "memory leak" and it may die if it runs out of memory. Edit your program and add the following code at "Location A" in the program.
// Return memory to O/S
delete [] array1;
Step 3: Compile and run your program. It should print out the same information as before. Since we are releasing the memory for array1 before we are allocating array2 there is some chance that the O/S will reuse some or all of array1 memory for array2. To see if this is true, you can edit the body of the array2 print loop as follows:
cout << array2[i]<<"";
array2[i]=42;
Step 4: Recompile and run your program with size1=20, size2=40. Then try size1=20, size2=20. Finally try size1=40, size2=20. You should see that in some cases the initial values of array2 are equal to zero, and in other cases they are equal to the values we stored in array1, proving the memory has been reused.
Step 5: In step 2 we returned the array1 memory back to the O/S. Since we have not changed this pointer, it still points to the original chunk of memory we allocated at the top of the program. For this reason, array1 is called a dangling reference -- it points to memory that was valid at one time, but the O/S thinks this pointer is no longer in use. To see what is now in in array1, add the following code at "Location B" in the program.
// Print array1
cout << "array1:
";
for (int i =0; i < size1; i++)
cout << array1[i]<<"";
cout << endl;
Step 6: Recompile and run your prog

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 Programming Questions!