前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >为什么告警warning implicit declaration of function必须修复

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

作者头像
mingjie
发布2023-03-14 09:51:29
1K0
发布2023-03-14 09:51:29
举报

相关:《C指针截断问题(形如0xaaaabbbbccccdddd > 0xffffffffccccdddd)》 本篇送给每一位写C/C++的朋友。

实例

试着编译这样一段程序

代码语言:javascript
复制
#include <stdio.h>

int *arr;

int main()
{
   arr = malloc(sizeof(int));
   return 0;
}

编译报错:

代码语言:javascript
复制
$ 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

代码语言:javascript
复制
$ 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 | 
代码语言:javascript
复制
$ 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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-03-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实例
  • 问题
  • 解决:建议把告警升级为报错(-Werror)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档