尝试将文本文件加载到动态分配的2d数组时出现"分段错误"错误

Spa*_*ago 0 c++ text-files multidimensional-array

我不确定究竟是什么导致了这个错误,因为代码工作了一段时间,但我必须改变一些搞砸了它的东西,我再也无法让它工作了.

这是加载到2d数组中的文本文件中的数据:

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 255 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是数组的长度/高度.imagecorrupted.txt与上面相同,但它在数据中有一个355而不是255某个地方.

这是我到目前为止提出的相关代码:

int** load(string imageFile, int &length, int &height) {
    ifstream file(imageFile);
    if(file.is_open()) {
        file >> length; // Loads 10 into length
        file >> height; // Loads 8 into height
        int** array = new int*[height];
        for(int i = 0; i < height; i++) {
            array[i] = new int[length];
        }
        for(int i = 0; i < height; i++) {
            for(int j = 0; j < length; j++) {
                file >> array[i][j];
                if(array[i][j] > 255 || array[i][j] < 0) {
                    cout << "Image is corrupted." << endl;
                    file.close();
                    return nullptr;
                }
            }
        }
        file.close();
        return array;
    }
    else {
        cout << "Unable to open file." << endl;
        return nullptr;
    }
}

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 < height; i++) {
        for(int j = 0; j < length; j++) {
            cout << " " << image[i][j]; 
        }
        cout << endl;
    }
}

void invert(int **image, int length, int height) {
    for(int i = 0; i < height; i++) {
        for(int j = 0; j < length; j++) {
            if(image[i][j] == 255) {
                image[i][j] = 0;
            }
            else {
                image[i][j] = 255;
            }
        }
        cout << endl;
    }
}

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

int main() {
    int height = 0;
    int length = 0;
    int** image = 0;
    image = load("../resource/imagecorrupted.txt", length, height);
    image = load("../resource/image.txt", length, height);
    show(image, length, height);
    invert(image, length, height);
    show(image length, height);
    free(image, length, height);
}
Run Code Online (Sandbox Code Playgroud)

输出:

Image is corrupted.
The height of the matrix is: 8
The length of the matrix is: 10
The matrix is: 
 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 255 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
// bunch of whitespace
-bash: line x: xxxxx Segemntation fault

我应该补充一点,这是一个来自类的赋值,因此我有一些限制在做的事情(例如,需要是2d数组而不是向量).

rsj*_*ffe 5

你交换length,并height在功能上show.外环应该height和内部一样length.