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

时间:2019-02-19 08:05:02

标签: c++ multidimensional-array io segmentation-fault text-files

当我尝试运行代码时,它可以毫无问题地进行编译。但是,当我尝试运行该程序时,得到line x: segmentation fault,x是错误所在的行,但是每次我尝试再次运行该程序时,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);

1 个答案:

答案 0 :(得分:2)

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

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

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

编辑

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

相关问题