我不能理解我从以下代码中得到的分段错误:
#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教程?
发布于 2011-04-29 21:44:47
这是完全未定义的行为-您取消引用未初始化的指针。
真正的问题在
printf( "%lld %lld\n", *w, *h);其他的事情只是声明。您不应该取消对w和h的引用,因为它们根本没有初始化。这与注释/取消注释任何前(3)行无关。
https://stackoverflow.com/questions/5832853
复制相似问题