本篇送给每一位写C/C++的朋友。
实例
试着编译这样一段程序
#include <stdio.h>
int *arr;
int main()
{
arr = malloc(sizeof(int));
return 0;
}
编译报错:
$ gcc -g a.c
a.c: In function 'main':
a.c:7:10: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
7 | arr = malloc(sizeof(int));
| ^~~~~~
a.c:7:10: warning: incompatible implicit declaration of built-in function 'malloc'
a.c:2:1: note: include '<stdlib.h>' or provide a declaration of 'malloc'
1 | #include <stdio.h>
+++ |+#include <stdlib.h>
问题
问题一:为什么编译器不报错?
- malloc在stdlib.h中声明,没有include为什么编译器没有报错。C Standard还没发布的时候,是允许不声明、不定义就使用函数的。为了适配这些老代码编译器是比较宽松的。
问题二:不管这个告警会发生什么?
- 编译器会从提供的参数类型中推断出函数的prototype(定义)。在上面例子中,malloc被推断为:
int malloc(int, size_t)
,这是非常危险的!- 因为函数指针从int64被截断为int32,而你没收到任何错误提示(只有告警),只有在运行时才会踩坑(发现指针截断了)。
- 运行时也不一定总会报错,因为如果截断后,指针地址在低地址,32位int能装下,程序是没有任何问题的。
- 但是一旦函数返回地址在高地址,截断后的int32指向就会有问题,运气好是个非法地址会core,运气不好直接拿到一个能写的地址,就把别的内存踩坏了,非常难查,因为报错的位置逻辑本身应该没有任何问题,内存是被隔空踩坏了!
解决:建议把告警升级为报错(-Werror)
即从gcc -g a.c
升级为 gcc -g a.c -Werror
。
$ gcc -g a.c
a.c: In function 'main':
a.c:7:10: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
7 | arr = malloc(sizeof(int));
| ^~~~~~
a.c:7:10: warning: incompatible implicit declaration of built-in function 'malloc'
a.c:2:1: note: include '<stdlib.h>' or provide a declaration of 'malloc'
1 | #include <stdio.h>
+++ |+#include <stdlib.h>
2 |
$ gcc -g a.c -Werror
a.c: In function 'main':
a.c:7:10: error: implicit declaration of function 'malloc' [-Werror=implicit-function-declaration]
7 | arr = malloc(sizeof(int));
| ^~~~~~
a.c:7:10: error: incompatible implicit declaration of built-in function 'malloc' [-Werror]
a.c:2:1: note: include '<stdlib.h>' or provide a declaration of 'malloc'
1 | #include <stdio.h>
+++ |+#include <stdlib.h>
2 |
cc1: all warnings being treated as errors