程序崩溃c0000374

Visual Studio 也给出了这些警告。写入“array1”时缓冲区溢出:可写大小为“1 8”字节,但可能会写入“16”字节。写入“array2”时缓冲区溢出:可写大小为“1 8”字节,但可能会写入“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;

    }

回答

线条

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

不是分配数组而是分配单个double初始化为charCountForA1and charCountForA2

使用[]而不是()分配数组。

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

  • Ending with a recommendation for using `std::vector` would help complete this answer.

以上是程序崩溃c0000374的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>