c++ - 尝试将文本文件加载到动态分配的二维数组时出现 bad_array_new_length 错误

标签 c++ multidimensional-array text-files bad-alloc

所以我在使用这段代码时遇到的问题是,当我运行它时,出现错误:terminate called after throwing an instance of "std:bad_array_new_length" what(): std:: bad_array_new_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

The first two values (10/8) are the length(rows) and height(columns) and the rest are meant to be stored in the 2d array.

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

我应该补充一点,这是一项家庭作业,因此我能做的事情非常有限。在 main 中,我确实需要将 load 中的值存储到 **image 中,以及为我设置的函数参数,并且是不可更改的。此外,它在类里面还处于早期阶段,因此对于此作业,我们不希望使用结构或类或类似的东西。感谢您给我的任何帮助/建议!

编辑:

感谢@IgorTandetnik 与我讨论我的问题,我找到了解决方法。如果将来有人遇到同样的问题并需要帮助,这个新代码可以工作:

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);
}

最佳答案

int** array = new int*[length] 此时,length 是一个未初始化的变量。您的程序表现出未定义的行为。

实际上,length 可能包含一个非常大的垃圾值。尝试分配这么大的内存块会失败。

关于c++ - 尝试将文本文件加载到动态分配的二维数组时出现 bad_array_new_length 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54757422/

相关文章:

c++ - 递归地包含头文件以进行合成

c++ - SetWindowsHookEx() ,钩子(Hook)不维护? (可能)

c++ - Qt: "No such signal"错误

c++ - C++中的多线程迭代

arrays - 如何折叠一组特定维度的数组?

c++ - 查找二进制二维数组中的唯一行

multidimensional-array - 在 clojure 中将多维数组转换为嵌套向量?

c - 我在带有分隔符的文本文件中有一组值如何使用 C 编程修改值

perl - 使用 Perl 解析文本文件的最有效方法是什么?

android - android 中文本文件的 View ,如 android 中图像的 ImageView