尝试打印/加载二维数组时出现分段错误

斯帕戈

当我尝试运行我的代码时,它编译没有问题。但是,当我尝试运行它时,我得到line x: segmentation fault, x 是错误所在的行,但是每次我尝试再次运行该程序时它都会更改 +1,这看起来很奇怪。下面是相关代码:

#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) {
    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];
                if(array[i][j] > 255 || array[i][j] < 0) {
                    cout << "Image is corrupted." << endl;
                    file.close();
                    return 0;
                }
            }
        }
        file.close();
        return array;
    }
    else {
        cout << "Unable to open file." << endl;
        return 0;
    }
}

void show(int **image, int length, int height) {
    cout << "The height of the matrix is: " << height << endl;
    cout << "The length of the matrix is: " << length << endl;
    cout << "The matrix is: " << endl;
    for(int i = 0; i < length; i++) {
        for(int j = 0; j < height; j++) {
            cout << " " << image[i][j]; 
        }
        cout << endl;
    }
}

int main() {
    int height = 0;
    int length = 0;
    int **image = load("../resource/imagecorrupted.txt", length, height);
    image = load("../resource/image.txt", length, height);
    show(image, length, height);
}

这是输出: Image is corrupted. Image is corrupted. //Not sure why this shows twice to be honest The height of the matrix is: 8 // but that seems like the least of my worries The length of the matrix is: 10 The matrix is: -bash: line xx: xxxxx Segmentation fault

不知道是什么原因造成的,任何帮助表示赞赏!

编辑:

我完全忘记了显示输入的含义。我道歉。他们来了: 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

这就是包含在image.txt. imagecorrupted.txt是相同的,只有一个值从 255 切换到 355(有意失败)。108是矩阵的长度/高度。

编辑2:

试图delete在每次load调用之间添加一个函数,但无济于事,尽管我确定这里有一些我没有得到的东西。这是使用的代码:

void free(int **image, int &length, int &height) {
    if(image) {
        for(int i = 0; i < length; i++) {
            if(image[i]) {
                delete[] image[i];
            }
        }
        delete[] image;
    }
}

我所做的主要是:

 int **image = load("../resource/imagecorrupted.txt", length, height);
 free(image, length, height);
 image = load("../resource/image.txt", length, height);
迈克尔·维克斯勒

首先,你有内存泄漏。为了避免泄漏并进行更好的边界检查,您应该考虑使用std::vector<std::vector<int>>而不是int**.

您的崩溃是由于第二次失败。当第二个load失败时,它返回0,即nullptrnullptr在这种情况下建议使用而不是0)。后来,show尝试取消引用它nullptr- 导致分段错误。

如果您坚持使用原始指针,而不是向量,或者第二好的 unique_ptr,那么您必须确保在 失败时load以及在连续成功调用之间load(和最后)清除分配

编辑

由于整数 355,第二个调用已损坏。此外,您的列和行似乎被调换了(行被视为列,列被视为行)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

对二维数组使用 malloc 时出现分段错误

尝试在 C 中访问二维数组的地址并出现分段错误

为什么在将双指针用作二维数组的参数时出现“分段错误”?

访问二维向量数组时出现分段错误/超出范围

将文件读取到二维 c 字符串数组后尝试进行任何操作时出现 C++ 分段错误

尝试打印二维数组(矩阵)时出错

尝试打印二维数组时程序崩溃

二维数组中的分段错误

尝试传递二维数组时出现不兼容的类型错误

尝试获取二维数组的平均值时出现逻辑错误

尝试动态重新分配二维数组时出现段错误

迭代二维字符数组时打印值错误

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

尝试反向打印数组时出现c ++分段错误

二维数组以错误的方式打印

在 Microsoft Visual Studio 2019 中在二维向量中插入元素时出现分段错误

为什么在尝试选择从 cursor.fetchall() 创建的二维数组中的列时出现错误?

c ++)将一维数组放入二维数组时出现错误

尝试打印结构时出现分段错误

二维数组分配后的分段错误

动态二维数组-缺少分段错误?

尝试将数据从列表复制到二维数组时出现 System.IndexOutOfRangeException

打印二维数组

加载二维数组

Perl:在运行时构建二维数组时出现内存不足错误

循环二维数组javascript时出现问题

尝试打印数组的第一个值时出现分段错误

分段错误:尝试打印包含 50k 项的数组时出现 11

尝试打印时出现垃圾值和分段错误