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

时间:2019-02-20 19:47:59

标签: c++ multidimensional-array text-files

我不确定是什么导致了此错误,因为代码工作了一段时间,但是我必须更改了一些混乱的东西,而我再也无法使其正常工作。< / p>

这是文本文件中正在加载到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);
}

输出:

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数组而不是向量)。

1 个答案:

答案 0 :(得分:5)

You've swapped length and height in function show. The outer loop should be height and the inner length.