提问者:小点点

程序崩溃,c0000374


visual studio也会给出这些警告。写入“array1”时缓冲区溢出:可写大小为“18”字节,但可能写入“16”字节。写入“array2”时缓冲区溢出:可写大小为“18”字节,但可能会写入“16”字节。

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

    }

共1个答案

匿名用户

台词

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

不分配数组,而是分配初始化为的单个

使用而不是分配数组。

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