指针无效(malloc)

时间:2017-04-22 10:01:47

标签: c sigabrt

当我尝试运行成功编译的程序时,我收到此错误:

*** Error in `./sharpen': free(): invalid pointer: 0x0000000000e7c030 ***
Neúspěšně ukončen (SIGABRT) (core dumped [obraz paměti uložen])

我正在努力研究代码,似乎无法找到任何可能导致此错误发生的内容。

这是一段代码:

unsigned char* apply_filter(unsigned char *original, header_t header) {
    unsigned char *pixel_array = malloc(header.width * header.height * 3);

    if(pixel_array == NULL) {
        fprintf(stderr, "Unexpected memory error! Out of memory!\n");
        free(pixel_array);
        exit(3);                // Memory was exceeded or something else happened to memory
    }

    for(int i = 0; i < header.height; ++i) {
        for(int j = 0; j < header.width; ++j) {
            if(i == 0 || i == header.height - 1 || j == 0 || j == header.width - 1) {
                pixel_array[3 * (header.width * i + j)]     = original[3 * (header.width * i + j)];
                pixel_array[3 * (header.width * i + j) + 1] = original[3 * (header.width * i + j) + 1];
                pixel_array[3 * (header.width * i + j) + 2] = original[3 * (header.width * i + j) + 2];
            } else {
                // middle pixel
                short pixel_red   = 5 * (short)original[3 * (header.width * i + j)];
                short pixel_green = 5 * (short)original[3 * (header.width * i + j) + 1];
                short pixel_blue  = 5 * (short)original[3 * (header.width * i + j) + 2];

                // backward and forward rows
                pixel_red   += -1 * (short)original[3 * (header.width * (i - 1) + j)];
                pixel_green += -1 * (short)original[3 * (header.width * (i - 1) + j) + 1];
                pixel_blue  += -1 * (short)original[3 * (header.width * (i - 1) + j) + 2];

                pixel_red   += -1 * (short)original[3 * (header.width * (i + 1) + j)];
                pixel_green += -1 * (short)original[3 * (header.width * (i + 1) + j) + 1];
                pixel_blue  += -1 * (short)original[3 * (header.width * (i + 1) + j) + 2];

                // backward and forward cols
                pixel_red   += -1 * (short)original[3 * (header.width * i + j - 1)];
                pixel_green += -1 * (short)original[3 * (header.width * i + j - 1) + 1];
                pixel_blue  += -1 * (short)original[3 * (header.width * i + j - 1) + 2];

                pixel_red   += -1 * (short)original[3 * (header.width * i + j + 1)];
                pixel_green += -1 * (short)original[3 * (header.width * i + j + 1) + 1];
                pixel_blue  += -1 * (short)original[3 * (header.width * i + j + 1) + 2];

                // put it all together now
                if(pixel_red < 0)
                    pixel_array[3 * (header.width * i + j)]     = 0;
                else if(pixel_red > 255)
                    pixel_array[3 * (header.width * i + j)]     = 255;
                else
                    pixel_array[3 * (header.width * i + j)]     = (unsigned char)pixel_red;

                if(pixel_green < 0)
                    pixel_array[3 * (header.width * i + j) + 1] = 0;
                else if(pixel_green > 255)
                    pixel_array[3 * (header.width * i + j) + 1] = 255;
                else
                    pixel_array[3 * (header.width * i + j) + 1] = (unsigned char)pixel_green;

                if(pixel_blue < 0)
                    pixel_array[3 * (header.width * i + j) + 2] = 0;
                else if(pixel_blue > 255)
                    pixel_array[3 * (header.width * i + j) + 2] = 255;
                else
                    pixel_array[3 * (header.width * i + j) + 2] = (unsigned char)pixel_blue;
            }
        }
    }

    return pixel_array;
}

请告诉我什么是错的。整个程序应加载PPM图像并通过卷积矩阵“锐化”它。发布的代码片段是锐化过程。图像采用PPM二进制RGB格式。

1 个答案:

答案 0 :(得分:0)

感谢您的讨论和提示,我忘了告诉您已经解决了问题:)

解决方案很简单:我只是忽略了一个错字:D我已经纠正了错字(以及更多代码中的错误数量)并且它的功能就像魅力:)我忘了添加一个=字符在less-or-equal条件下(类似于<=),因此它错误地填充数组并创建的数据数组少于所需数据。

感谢您参与解决问题,但是:)