首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试将文本文件加载到动态分配的2d数组时出现“分段错误”错误

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

Stack Overflow用户
提问于 2019-02-21 07:25:34
回答 1查看 0关注 0票数 0

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

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

代码语言:javascript
复制
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某个地方。

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

代码语言:javascript
复制
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);
}

输出:

代码语言:javascript
复制
图像已损坏。
矩阵的高度为:8
矩阵的长度为:10
矩阵是: 
 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
//一堆空白
-bash:line x:xxxxx Segemntation fault

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

EN

回答 1

Stack Overflow用户

发布于 2019-02-21 16:47:52

你交换lengthheightshow上。外环应该和内部一样length和height

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100008965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档