为什么告警warning implicit declaration of function必须修复

相关:《C指针截断问题(形如0xaaaabbbbccccdddd > 0xffffffffccccdddd)》

本篇送给每一位写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>

问题

问题一:为什么编译器不报错?

  1. malloc在stdlib.h中声明,没有include为什么编译器没有报错。C Standard还没发布的时候,是允许不声明、不定义就使用函数的。为了适配这些老代码编译器是比较宽松的。

问题二:不管这个告警会发生什么?

  1. 编译器会从提供的参数类型中推断出函数的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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高铭杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值