1

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

#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 教程?

4

1 回答 1

4

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

实际问题在

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

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

于 2011-04-29T13:44:46.930 回答