Dynamic Arrays: Need Help Copying One Array To Another One

Hello,


I have to create a program that uses the ‘new’ operator to create a dynamic array in heap of the program. The program creates and populates its dynamic array one (int) elements at a time for each input data (cin).

Key parts of the assignment.

Hello,

I have to create a program that uses the 'new' operator to create a dynamic array in heap of the program. The program creates and populates its dynamic array one (int) elements at a time for each input data (cin).

Key parts.

1.) Program has to used "cin >>" for data input to accept on integer at a time until EOF is pressed on the keyboard (cntrl-z for windows).

2.) User input has to be tested using !cin.eof() && cin.good() to test whether or not the EOF key was pressed and if the data is valid. (kinda of confused about the cin.good() part).

3.) The program will create a series of longer and longer arrays to contain all previous elements and the current incoming one. Also, the program will delete the previous version of the array after completing the current version.

4.) The program also tests if heap memory has been exhausted after each use of the new operator. (need help with this)

I keep getting error message "HEAP CORRUPTION DETECTOR After normal black (#146)" (visual studio). What's the issue?

Thanks in advance!

Here's the code:
Here’s what I have so far.

I keep getting error message “HEAP CORRUPTION DETECTOR After normal black (#146)” (visual studio). What’s the issue?


Thanks in advance



<#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cassert>

using namespace std;


// main
int main() {
int size = 2;
int * array1 = new int[size];
int arrayInput;
int count = 0;


do {


if (array1 != NULL) {

cout << "Enter an integer (EOF to stop): " << endl;
cin >> arrayInput;
if (size < count) {
int * tempArray;

tempArray = new int[size * 2];

if (tempArray != NULL)
{
for (int i = 0; i < size; i++) {
array1[i] = tempArray[i];
}
delete[] array1;
array1 = tempArray;
size *= 2;

delete [] tempArray;

}
else
cout << "Insufficient Heap resource." << endl; // If we get here the Heap is out of space
}
if (!cin.eof()) {
array1[count++] = arrayInput;
}
}
else
cout << "Insufficient Heap resource." << endl; // If we get here the Heap is out of space





} while (!cin.eof());


for (int i = 0; i < count; i++) {
cout << array1[i] << endl;
}



}


>
Last edited on
if new or new[] fail they will throw an exception. There is no point in checking for NULL.

For input you may use
1
2
3
while( std::cin>>input ){
  //...
}
it will stop at EOF or input error.


1
2
3
4
5
6
7
8
9
10
int* tempArray;
tempArray = new int[size * 2];
for (int i = 0; i < size; i++)
{
	array1[i] = tempArray[i]; //¿?
}
delete[] array1;
array1 = tempArray;
size *= 2;
delete[] tempArray; //¿? 
kindly explain your intention in the marked lines.
Hi,

It might be a good idea to surround your code in
1
2
3
4
5
6
7
8
9
 tags.  This helps make it more legible for us :)

There's a problem with this section:

[code]
array1 = tempArray;
size *=2;

delete[] tempArray; 


The problem here, is that both array1 and tempArray point to the same dynamically allocated array

When you use delete[] on tempArray, you are deleting the array that is being pointed to by both tempArray AND array1.

Then, later in your code, you try to access that array via array1 again, which causes a fail.

Fix that, first and see what other problems you get.

Joe:
sparkprogrammer@gmail.com
Topic archived. No new replies allowed.