c - C程序高4字节的指针地址异常
问题描述
在程序的一部分中,我以两种不同的方式打印出相同的指针。
vpx_codec_iface_t *ptr = vpx_codec_vp9_cx();
printf("ptr1 %p\n", ptr);
printf("ptr2 %p\n", vpx_codec_vp9_cx());
这奇怪地导致以下输出。
ptr1 FFFFFFFFDAF9CED0
ptr2 00000000DAF9CED0
使用该程序,我可以通过添加一些代码或添加一些换行符来“修复”错误。
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);
这将导致以下输出。
ptr1 0000000066A7CED0
ptr2 0000000066A7CED0
x=0
什么可能导致这种行为?我在 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);
}
解决方案
您的编译环境中是否打开了警告?这看起来很像您缺少 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;
}
myptr.c:
int myptr(void) {
return 0xd0000020;
}
注意 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.
我的编译器显然是冗长的,但关键是过去 30 年的每个编译器都应该报告某种诊断,您至少应该在忽略之前了解这些诊断。
推荐阅读
- python - 如何在 pandas DataFrame 中格式化特殊小数
- javascript - 为什么我不能在 JavaScript while 循环之外设置变量的值?
- javascript - 如何阻止把手执行字符串?
- reactjs - 未捕获的 SyntaxError:意外的标记 '<' | 在“纱线添加 xlsx”之后 | 反应打字稿
- flutter - 如何在堆栈内的缩小容器中正确居中文本小部件
- wpf - 设计时错误 Wpf ValidatesOnTargetUpdated NullReferenceException
- android - Kotlin 谷歌地图无法启动
- javascript - VSCODE Javascript模板字符串以不确定的方式变为白色并丢失突出显示
- c# - C# 反序列化 - 获取属性返回 null
- wpf - 如何在 wix 工具集引导程序项目中获取当前的 wpf 项目程序集版本?