首页 > 解决方案 > 程序崩溃并出现 c0000374

问题描述

视觉工作室也给出了这些警告。写入“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;

    }

标签: c++

解决方案


线条

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

不是分配数组,而是分配单个double初始化到charCountForA1charCountForA2

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

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

推荐阅读