C++ 尝试将文本文件加载到动态分配的二维数组时,出现错误数组长度错误
因此,我对这段代码的问题是,当我运行它时,我得到一个错误:C++ 尝试将文本文件加载到动态分配的二维数组时,出现错误数组长度错误,c++,multidimensional-array,text-files,bad-alloc,C++,Multidimensional Array,Text Files,Bad Alloc,因此,我对这段代码的问题是,当我运行它时,我得到一个错误:在抛出“std:bad\u array\u new\u length”what()实例后调用terminate():std::bad\u array\u new\u length否则没有错误,代码编译得很好。下面是我尝试加载的文本文件: 10 8 0 255 255 255 0 0 255 255 255 0 255 0 255 255 0 0 255 255 0 255 255 255 0 255 25
在抛出“std:bad\u array\u new\u length”what()实例后调用terminate():std::bad\u array\u new\u length
否则没有错误,代码编译得很好。下面是我尝试加载的文本文件:
10 8
0 255 255 255 0 0 255 255 255 0
255 0 255 255 0 0 255 255 0 255
255 255 0 255 255 255 255 0 255 255
255 255 255 0 255 255 0 255 255 255
255 255 255 355 0 0 255 255 255 255
255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
0 0 0 255 255 255 255 0 0 0
10 8
0 255 255 255 0 0 255 255 255 0
255 0 255 255 0 0 255 255 0 255
255 255 0 255 255 255 255 0 255 255
255 255 255 0 255 255 0 255 255 255
255 255 255 355 0 0 255 255 255 255
255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
0 0 0 255 255 255 255 0 0 0
前两个值(10/8)是长度(行)和高度(列),其余值将存储在二维数组中
#include <iostream>
#include <fstream>
#include <string>
#include "image.h" // Has the prototypes for the functions
using namespace std;
int** load(string imageFile, int &length, int &height) {
int** array = new int*[length];
ifstream file(imageFile);
if(file.is_open()) {
file >> length; // Takes the first value and stores it as length
file >> height; // Takes the second value and stores it as height
for(int i = 0; i < length; i++) {
array[i] = new int[height];
for(int j = 0; j < height; j++) {
file >> array[i][j];
}
}
}
else {
cout << "Unable to open file." << endl;
return 0;
}
return array;
}
int main() {
int height, length;
int **image = load("../resource/imagecorrupted.txt", length, height);
}
#包括
#包括
#包括
#包括“image.h”//具有功能的原型
使用名称空间std;
整数**加载(字符串图像文件、整数和长度、整数和高度){
整数**数组=新整数*[长度];
ifstream文件(imageFile);
if(file.is_open()){
file>>length;//获取第一个值并将其存储为length
file>>height;//获取第二个值并将其存储为height
for(int i=0;i>数组[i][j];
}
}
}
否则{
coutint**array=new int*[length]
此时,length
是一个未初始化的变量。您的程序显示未定义的行为
实际上,length
可能包含一个非常大的垃圾值。尝试分配这么大的内存块将失败。您还应该熟悉如何使用调试器。如果您使用调试器逐步完成代码,问题将非常明显。我们不希望使用结构或类--您是使用std::string
和std::ifstream
。是的。谢谢@igortandtnik的回复。不知道我怎么会错过。但是现在,我得到了一个双重免费或损坏的错误。我不确定你是否知道是什么原因造成的,但不管怎样,我感谢你的回答。在“当时”和“现在”之间发生了什么变化?我的水晶球最近阴云密布。对不起,我真的应该说我所做的。我所做的只是将main
中的length
和height
初始化为0,因为它们在load
函数中得到了实际值。希望我没有误解您给我的修复,尽管这样做确实修复了初始错误我有。那么。int**array=new int*[length]
分配一个长度为0的数组,然后array[I]
通过访问一个超出范围的索引来显示未定义的行为。啊。看来我误解了你给我的最初答案……我会做一些测试,然后再给你。谢谢你的帮助!
int** load(string imageFile, int &length, int &height) {
ifstream file(imageFile);
if(file.is_open()) {
file >> length;
int** array = new int*[length];
file >> height;
for(int i = 0; i < length; i++) {
array[i] = new int[height];
for(int j = 0; j < height; j++) {
file >> array[i][j];
}
}
return array;
}
else {
cout << "Unable to open file." << endl;
return 0;
}
}
int main() {
int height = 0;
int length = 0;
int **image = load("../resource/imagecorrupted.txt", length, height);
}