在程序的一部分中,我以两种不同的方式打印出相同的指针。
vpx_codec_iface_t *ptr = vpx_codec_vp9_cx();
printf("ptr1 %p\n", ptr);
printf("ptr2 %p\n", vpx_codec_vp9_cx());
Run Code Online (Sandbox Code Playgroud)
这奇怪地导致以下输出。
ptr1 FFFFFFFFDAF9CED0
ptr2 00000000DAF9CED0
Run Code Online (Sandbox Code Playgroud)
玩弄这个程序,我可以通过添加一些代码或添加一些换行符来“修复”错误。
int x = 0;
vpx_codec_iface_t *ptr = vpx_codec_vp9_cx();
printf("ptr1 %p\n", ptr);
printf("ptr2 %p\n", vpx_codec_vp9_cx());
printf("x=%d\n", x);
Run Code Online (Sandbox Code Playgroud)
这导致以下输出。
ptr1 0000000066A7CED0
ptr2 0000000066A7CED0
x=0
Run Code Online (Sandbox Code Playgroud)
什么可能导致这种行为?我在 Windows 10 上使用 Visual Studio 2019 编译器,为 x64 进行编译。函数调用vpx_codec_vp9_cx()是在vpxmd.lib其中实现的,来自libvpx项目。
编辑:我仍在查看您的答案和评论,但我在下面创建了一个最小示例。不幸的是,它涉及构建整个 vpx 库,因此我需要一些时间来简化该部分。
#include <stdio.h>
#include "vpx/vpx_encoder.h"
int main(int argc, char **argv) {
printf("This is main\n");
vpx_codec_iface_t *ptr = vpx_codec_vp9_cx();
int x = 0;
printf("ptr1 %p\n", ptr);
printf("ptr2 %p\n", vpx_codec_vp9_cx());
printf("x=%d\n", x);
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
您的编译环境中是否打开了警告?这看起来很像您缺少 vpx_code_vp9_cx() 的原型。在第一种情况下,分配给 ptr,预期类型强制将符号扩展(默认)int 值 vpx_codex_vp9_cs()。在第二种情况下,printf 将保持不变。一个更简单的例子可以用:print.c:
#include <stdio.h>
int main() {
void *x = myptr();
printf("x = %p\n", x);
printf("myptr() = %p\n", myptr());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
myptr.c:
int myptr(void) {
return 0xd0000020;
}
Run Code Online (Sandbox Code Playgroud)
请注意,print.c 没有声明 myptr()。在我的系统上,一个简单的编译:cc print.c myptr.c -op产生:
print.c:6:12: warning: implicit declaration of function 'myptr' is invalid in C99
[-Wimplicit-function-declaration]
void *x = myptr();
^
print.c:6:8: warning: incompatible integer to pointer conversion initializing
'void *' with an expression of type 'int' [-Wint-conversion]
void *x = myptr();
^ ~~~~~~~
print.c:8:27: warning: format specifies type 'void *' but the argument has type
'int' [-Wformat]
printf("myptr() = %p\n", myptr());
~~ ^~~~~~~
%d
3 warnings generated.
Run Code Online (Sandbox Code Playgroud)
我的编译器显然是冗长的,但关键是过去 30 年的每个编译器都应该报告某种诊断,您至少应该在忽略之前了解这些诊断。