c - libjpeg 的段错误

标签 c segmentation-fault libjpeg

我无法理解我从以下代码中得到的段错误:

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

int main(int argc, char** argv){
    FILE*                   outfile;
    JSAMPLE*                row_pointer;
    struct jpeg_error_mgr   jerr;
    long long int           *w, *h;

    setSomePointers(w, h);

    printf( "%lld  %lld\n", *w, *h);
}

注释掉前三个声明中的任何一个都可以修复它...

奇怪的是,下面的代码有效:

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

int main(int argc, char** argv){
    FILE*                   outfile;
    JSAMPLE*                row_pointer;
    struct jpeg_error_mgr   jerr;
    long long int           w, h;

    setSomePointers(&w, &h);

    printf( "%lld  %lld\n", w, h);
}

有没有什么奇怪的事情发生,或者我需要打一些C教程?

最佳答案

这完全是未定义的行为 - 您取消引用未初始化的指针。

实际问题在

printf( "%lld  %lld\n", *w, *h);

其他的只是声明。您不应该取消引用 wh,因为它们根本没有被初始化。这与评论/取消评论任何前 (3) 行无关。

关于c - libjpeg 的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5832853/

相关文章:

c++ - 将 YCCK jpeg 转换为 RGB

c - 为什么这个动态加载的库不能访问加载程序的全局变量?

c++ - `undefined reference` 尝试连接 C 以调用 C++

C 程序在尝试写入 stdout 后退出

c++ - 您如何阅读 segfault 内核日志消息

c - 在 C 中将字符串存储到 Char 数组中 & 段错误

c - 在此函数中使用未初始化的数组

python - 如何安装libjpeg?

在 C/UNIX 中创建子进程/杀死进程

MATLAB - 编译 jpeg_read.c 创建 mexmaci64 文件时出错