c - 使用 libjpeg 读取图像像素数据的 3 维数组的内存分配

标签 c pointers memory-management user-defined-functions

我正在尝试使用 libjpeg 将图像的像素数据复制到矩阵。指针出了问题。请帮忙。

问题简述:我将一个 int ***p 指针传递给读取像素数据的函数。在函数中,我可以访问元素 p[i][j][k] 并对它们执行操作,但是当我尝试在 main 中执行相同操作时程序崩溃。 主要功能是:

#include<stdio.h>
#include<stdlib.h>
#include<jpeglib.h>
#include"functions.h"

void main(void)
{
    char * file="a.jpg";
    int ***p;                 // to store the pixel values
    int s[2]={0,0};           // to store the dimensions of the image
    read_JPEG_file(file,p,s); // Function that reads the image
    printf("%d",p[0][0][0]);  // This makes the program crash
}

文件functions.h内容如下:

int read_JPEG_file(char * file, int ***p, int **s)
{
    int i,j;
    //-----------------libjpeg procedure which I got from the documentation------------
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    cinfo.err=jpeg_std_error(&jerr);

    FILE * infile;      /* source file */
    JSAMPARRAY buffer;      /* Output row buffer */
    int row_stride;
    if ((infile = fopen(filename, "rb")) == NULL) 
    {
        fprintf(stderr, "can't open %s\n", filename);
        return 0;
    }

    jpeg_create_decompress(&cinfo);
    jpeg_stdio_src(&cinfo, infile);
    jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress(&cinfo);
    row_stride = cinfo.output_width * cinfo.output_components;
    buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
    s[0]=cinfo.output_height;
    s[1]=cinfo.output_width;
    p=(int ***)malloc(s[0]*sizeof(int **));
    for (i=0;i<s[0];i++)
    {
        p[i]=(int **)malloc(s[1]*sizeof(int *));
        for (j=0;j<s[1];j++)
        {
            p[i][j]=(int *)malloc(3*sizeof(int));
        }
}

while (cinfo.output_scanline < cinfo.output_height) 
    {
    jpeg_read_scanlines(&cinfo, buffer, 1);
        for ( i=0; i<cinfo.output_width; i++)
        {
             p[cinfo.output_scanline-1][i][0]=(int)buffer[0][0+3*i];
             p[cinfo.output_scanline-1][i][1]=(int)buffer[0][1+3*i];
             p[cinfo.output_scanline-1][i][2]=(int)buffer[0][2+3*i];
         }
    }

printf("%d",p[0][0][0]); // This works just fine
return 0;
}

我知道内存分配有问题,但我不知道是什么问题。我尝试了另一个测试程序并成功分配了一个 500X500X500 整数长的内存块,并且它正在工作 - 它输出随机整数而不会崩溃 - 因此内存不足不是问题。

最佳答案

你的 int ***p 不是 malloc。 当您将他发送到 read_JPEG_file 时,创建了第二个变量 int ***p 。 第二个变量在 read_JPEG_file 末尾被破坏。所以你可以使用 p 在 read_JPEG_file 中但仅此而已。 有三种方法可以解决。 第一个我认为更容易理解的是返回p。

 int ***read_JPEG_file(char * file, int ***p, int **s) {
     ...
     return p; 
 }

 int main() {
    ...
    p = read_JPEG_file(file, p, s);
 }

(不再需要发送p)

第二种方式是在main中malloc p然后发送他。 对于你的情况来说,这似乎有点难做到。

第三个是在main中发送p的地址。你将得到一个 (int * * * *p)。 使用时一定要小心。 你可以做这样的事情:

 int read_JPEG_file(char * file, int ****p, int **s)
 {
     ...
     *p=(int ***)malloc(s[0]*sizeof(int **));
 }

现在您必须使用 *p 而不是 p。 *p表示包含指针p的值。 p是一个int * * * *所以他的值就是int * * *。

 int main()
 {
     ...
     read_JPEG_file(file, &p, s);
 }

关于c - 使用 libjpeg 读取图像像素数据的 3 维数组的内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19155346/

相关文章:

c - 在 C 语言中使用 LEDS 进行二进制仿真

c - 如何修复 Command/Developer/usr/bin/clang 失败,退出代码为 1?

c - (linux) "Segmentation fault (core dumped)"将 char 指针传递给 c 中的函数

c - typedef 函数指针类型

c - K&R 书末描述的存储分配器中的 "space itself"在哪里?

c - 可变大小数组的内存分配机制

C 如何在 curses 程序结束后让屏幕恢复正常

c++指针的内存分配过程

c - 如何修复非标量类型转换错误

mysql - 如何知道 MySQL 何时达到服务器的内存限制?