Benuka Punchihewa
Benuka Punchihewa

Reputation: 1

program crashes with c0000374

visual studio also gives these warning. Buffer overrun while writing to 'array1': the writable size is '18' bytes, but '16' bytes might be written. Buffer overrun while writing to 'array2 ': the writable size is '18' bytes, but '16' bytes might be written.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int charCountForA1, charCountForA2;

    cout << "How many character do you want for Array 1 ? ";
    cin >> charCountForA1;

    cout << endl;

    cout << "How many character do you want for Array 2 ? ";
    cin >> charCountForA2;

    //dynamic declaration of memory
    double* array1 = new double(charCountForA1);
    double* array2 = new double(charCountForA2);

    cout << endl;

    //to get user inputs to fill the array 1
    for (int n = 0; n < charCountForA1; n++) {

        double x;
        cout << "Enter the element for index " << n << " of Array 1: ";
        cin >> x;
        array1[n] = x;

    }

    cout << endl;

    //to get user inputs to fill the array 1
    for (int n = 0; n < charCountForA2; n++) {

        double x;
        cout << "Enter the element for index " << n << " of Array 2: ";
        cin >> x;
        array2[n] = x;

    }

Upvotes: 0

Views: 109

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

The lines

    double* array1 = new double(charCountForA1);
    double* array2 = new double(charCountForA2);

are not allocating arrays but allocating single double initialized to charCountForA1 and charCountForA2.

Use [] instead of () to allocate arrays.

    double* array1 = new double[charCountForA1];
    double* array2 = new double[charCountForA2];

Upvotes: 5

Related Questions