c++ - 程序崩溃并出现 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;
}
解决方案
线条
double* array1 = new double(charCountForA1);
double* array2 = new double(charCountForA2);
不是分配数组,而是分配单个double
初始化到charCountForA1
和charCountForA2
。
使用[]
而不是()
分配数组。
double* array1 = new double[charCountForA1];
double* array2 = new double[charCountForA2];
推荐阅读
- bazel - 我正在将项目从 maven 迁移到 bazel 并出现错误
- ruby-on-rails - Rails web-push 抛出错误 401(未经授权)
- python-3.x - ModuleNotFoundError:没有名为“mailmerge”python 的模块
- design-patterns - 如何更新多个页面中某些值的使用单位
- amazon-web-services - SQS FIFO 发送消息失败
- azure - Set-AzDiagnosticSetting - 跳过 Azure 数据块的“metricsCategory”
- android - 裁剪图像在裁剪器移动时获取高度和宽度
- r - dplyr - 使用列索引而不是列名将列输入到 rowwise()
- javascript - 使用 SocketIO 发送消息失败,也没有在窗口或另一侧更新
- vagrant - 执行 `VBoxManage` 时出错(VBoxManage:错误:详细信息:代码 NS_ERROR_FAILURE (0x80004005),组件)