【问题标题】:Reading a JPEG file with a buffer : segfault读取带缓冲区的 JPEG 文件:segfault
【发布时间】:2015-11-20 11:51:14
【问题描述】:

基本上我正在尝试为 JPEG 文件编写阅读器(使用 libjpeg),但我有一个段错误,我想知道是否有人能看到我的错误在哪里?我确定它来自malloc,但我不知道。我试图通过放置一些 printf 来进行一些调试,但它们绝对没有出现,wtf?

感谢您的帮助伙伴...

ma​​in.c:

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


int main (int argc, char** argv){


    int *H;
    int *W;
    int *C;
    printf("COUCOUCOUCOUCOU");
    FILE *fichier = NULL;
    char car;

    fichier = fopen("cara.jpg", "r");

    if (fichier == NULL)
        printf("Probleme lecture");


    lire(fichier, H, W, C);

    fclose(fichier);
    return 0;
}   

lire.c:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <jpeglib.h>
#include <jerror.h>

unsigned char** lire (FILE* file, int *H, int *W, int *C){

struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;

int n = 0;
unsigned char** buffer;

printf("SHITSHITSHITSHIT\n");
fflush(stdout);
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo); // Initialisation de la structure

jpeg_stdio_src(&cinfo,file);  // file est de type FILE * (descripteur de fichier
                              // sur le fichier jpega decompresser)
jpeg_read_header(&cinfo,TRUE);// lecture des infos sur l'image jpeg


jpeg_start_decompress(&cinfo);// lancement du processus de decompression


*H = cinfo.output_height;
*W = cinfo.output_width;
*C = cinfo.output_components;

buffer=(unsigned char **)malloc( (*H) *sizeof(unsigned char*) );

while (n < *H)
 {
    buffer[n] = (unsigned char*) malloc( (*W) * (*C) *sizeof(unsigned char *) );
        printf("DEBUG\n");
    fflush(stdout);
     jpeg_read_scanlines(&cinfo,buffer+n,1); // lecture des n lignes suivantes de l'image
                                          // dans le buffer (de type unsigned char *)
     n++;
}

jpeg_finish_decompress(&cinfo);

jpeg_destroy_decompress(&cinfo);

return buffer;
}

我编译:

gcc -c -I/usr/local/include *.c
gcc *.o -o programme -ljpeg

【问题讨论】:

  • 尝试刷新标准输出,以便您的(缓冲的)调试语句在程序崩溃之前出现,例如printf("COUCOUCOUCOUCOU"); fflush(stdout);
  • 好的,所以我在代码的开头放了一个“fflush(stdout)”?
  • lire 函数在哪里?
  • 天哪,对不起!里拉功能是“阅读”!我翻译了它,但它是公元前“里拉”在法语中的意思是“阅读”:)(我编辑过)
  • 您是否意识到您已经发布了两次相同的代码?即main.clire.c 是相同的。我认为您的问题中缺少来自lire.c 的代码。

标签: c libjpeg


【解决方案1】:

当您调用lire 时,您将H、W 和C 传递给函数。这些是指针,它们没有被初始化,因此你会在lire 中遇到崩溃。

*H = cinfo.output_height;

你需要像这样调用lire函数:

int H;
int W;
int C;

....

lire(fichier, &H, &W, &C);

【讨论】:

  • 哦,是的,我现在明白了!谢谢迈克尔!它不再出现段错误
猜你喜欢
  • 2013-01-21
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 2013-09-17
相关资源
最近更新 更多