试图打印出/加载2D数组时的分割故障

Segmentation fault when trying to print out/load a 2d array

本文关键字:数组 分割 故障 2D 加载 打印      更新时间:2023-10-16

当我尝试运行代码时,它没有任何问题。但是,当我尝试运行它时,我会得到 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:

尝试在每个load调用之间添加delete功能,但无济于事,尽管我确定这里有些东西。这是使用的代码:

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,即nullptr(建议在这种情况下使用nullptr而不是0)。后来,show试图取消此nullptr-导致分割故障。

如果您坚持使用原始指针,而不是向量,或者是第二好的,唯一的_ptr,则必须确保在load失败时以及连续成功的load(以及最后)的连续成功呼叫之间清理分配。

编辑

由于整数355,第二个呼叫被损坏。此外,您的列的和行似乎也被转移(行被视为列和列视为行)。