C:warning: null argument where non-null required (argument 2) [-Wnonnull]

main.c:950:32: warning: null argument where non-null required (argument 2) -Wnonnull

这个意思说,函数定义里面由一个attribute,标记第二个参数不能为空。

warning.c:25:2: warning: null argument where non-null required (argument 2) -Wnonnull

memcmp(&xia, 0, 0);

比如这个memcmp的第一个和第二个参数;

glibc的定义(string.h)如下:

c 复制代码
/* Compare N bytes of S1 and S2.  */
extern int memcmp (const void *__s1, const void *__s2, size_t __n)
     __THROW __attribute_pure__ __nonnull ((1, 2));

为什么要加这个属性?根据源代码看的话,就是在第三个参数不为0的情况下,如果s1和s2有一个是null,那么就会发生SegV。注意是给开发者一个提示,避免问题的发生。

c 复制代码
commit be27d08c05911a658949ba7b84f4321a65a2dbf4 (tag: cvs/fedora-glibc-20040923T0148)
Author: Ulrich Drepper <drepper@redhat.com>
Date:   Wed Sep 22 08:20:57 2004 +0000
    Update.
    2004-09-22  Ulrich Drepper  <drepper@redhat.com>
            * string/string.h: Add __nonnull annotations.
            * stdlib/stdlib.h: Likewise.
相关推荐
Zwawa5 小时前
屏幕不止一种:OLED、LCD1602字符屏、TFT/IPS图形屏和串口屏怎么用,这与TTY和PTY又有什么关系
c语言·单片机·嵌入式
Fruiticecake5 小时前
C 程序员速通现代 C++(一):lambda、std::function、移动语义与智能指针
c语言·c++
田甲5 小时前
做一款 Type-C 母座读卡器:GL3224 双卡方案实测
c语言·开发语言
·present·10 小时前
CSAPP(lecture 13_02 Linking)
c语言·c++
wuyk5551 天前
Keil工具链-第05讲-启动文件与__main到底做了什么
c语言·stm32·单片机·嵌入式硬件·物联网
Navigator_Z1 天前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
冻柠檬飞冰走茶1 天前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
Hhy_11072 天前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
曦樂~2 天前
Cpp面筋
c语言·c++
HUANGZHENXUAN1232 天前
最大子矩阵和:从 O(n⁴) 到 O(n³) 的列压缩 + Kadane
c语言·前端