printf %p 段错误
printf("\nframe is: %p",&frame);
printf("\nframeprev is: %p",&framePrev);
无论哪一行先出现,都将始终正确打印。 在上面的代码中,第二行总是会出现段错误,无论它打印的是哪个指针。有什么想法吗?我尝试过 fflush( stdout );在每个 printf 之后但这似乎没有什么区别。
使用以下命令释放指针
frame =(double**) malloc(cols*sizeof(double));
framePrev =(double**) malloc(cols*sizeof(double));
if(frame==NULL||framePrev==NULL){
printf("malloc epic fail\n");
return 0;
}
/*allocate mem for 2nd dimention*/
for(i=0;i<cols;i++){
frame[i]=(double*) malloc(rows*sizeof(double));
framePrev[i]=(double*) malloc(rows*sizeof(double));
/*check for null pointer*/
if(frame[i]==NULL||framePrev[i]==NULL){
printf("malloc epic fail\n");
return 0;
}
}
printf("\nframe is: %p",&frame);
printf("\nframeprev is: %p",&framePrev);
whatever line comes first will always correctly print.
The second line will always segfault in the above code, regardless of which pointer it is printing. any ideas whythis is? I have tried fflush( stdout ); after each printf but this seems to make no difference.
the pointer is delcaired using the following
frame =(double**) malloc(cols*sizeof(double));
framePrev =(double**) malloc(cols*sizeof(double));
if(frame==NULL||framePrev==NULL){
printf("malloc epic fail\n");
return 0;
}
/*allocate mem for 2nd dimention*/
for(i=0;i<cols;i++){
frame[i]=(double*) malloc(rows*sizeof(double));
framePrev[i]=(double*) malloc(rows*sizeof(double));
/*check for null pointer*/
if(frame[i]==NULL||framePrev[i]==NULL){
printf("malloc epic fail\n");
return 0;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道为什么您提供的代码会出现段错误。但是,您尝试打印的是指针的地址,而不是指针的内容。也就是说,frame是一个指针变量;它有 4 个字节大,位于堆栈/堆上的某个地方。你正在某处打印出该地址。我认为你想要的是打印框架的值;这将是框架所持有的实际指针。所以脱掉 &在每一行中看看你会得到什么。
另外,你的内存分配很奇怪。 A (double **) 是指向数组的指针,该数组保存指向双精度数的指针;但是您的 malloc 调用分配了一个包含双精度数的数组。您可能需要 malloc(cols * sizeof(double *))。这段代码是有效的,因为 double 比 double * 大,所以你实际上分配了足够的内存,但它仍然是错误的。
I don't know why that code you provided would segfault. However, what you're trying to print out is the address of the pointer, not the contents of the pointer. In other words, frame is a pointer variable; it is 4 bytes big and lives on the stack/heap somewhere. You're printing out the address of that somewhere. What I think you want is to print the value of frame; which would be the actual pointer that frame holds. So take off the & in each line and see what you get.
Also, your memory allocation is strange. A (double **) is a pointer to an array that holds pointers to doubles; but your malloc call allocates an array that holds doubles. You probably want malloc(cols * sizeof(double *)). This code works, because double is bigger than double *, so you actually allocate enough memory, but it's still wrong.
你的 printf 语句没有任何问题。如果出现段错误,可能是因为您的例程中存在错误并且破坏了内存。
您发布的内容中的一个明显问题是您的frame和frameprev数组不是双精度数组,而是双指针数组,并且它们不太可能具有相同的大小。前两行应该是:
There is nothing wrong with your printf statements. If you're segfaulting, its probably because you have a bug in your routines and have smashed memory.
One obvious problem in what you posted is that your frame and frameprev arrays are not arrays of doubles, but double pointers, and its unlikely that they are the same size. The first two lines should be:
只需将代码放入 test.cpp 文件中,如下所示:
通过 g++ test.cpp 编译,通过 ./a.out -> 运行它
根本不存在段错误。
你如何编译?您的帖子中现在显示了对您的代码的任何其他更改吗?
你如何初始化你的变量?
法规。
just throw the code into a test.cpp file like this:
compiled via g++ test.cpp, run it via ./a.out ->
there is no segfaulting at all.
how to you compile? any other changes to your code now shown in your post?
how do you initialize your variables?
regs.