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

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



}

Recommended Answers

All 2 Replies

You need #include <new>

This is when I drop back to C and use realloc() - much easier and sets errno appropriately if you run out of heap (returning NULL for the result of the call). No copying necessary - realloc() will allocate the new memory and copy data as necessary. Programming C++ doesn't mean you have to ignore C. Use the proper tool for the job!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.