在下面的声明中_Nullable是什么意思?
void foo(int *_Nullable ptr)
版本11.4的GCC没有编译该代码,因为它将_Nullable关键字视为参数名.然而,我在MAN 7页上看到了这个关键词: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html个 https://man7.org/linux/man-pages/man2/timer_gettime.2.html
在下面的声明中_Nullable是什么意思?
void foo(int *_Nullable ptr)
版本11.4的GCC没有编译该代码,因为它将_Nullable关键字视为参数名.然而,我在MAN 7页上看到了这个关键词: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html个 https://man7.org/linux/man-pages/man2/timer_gettime.2.html
_Nullable
simply means that the pointer passed to the function is allowed to be NULL
. It is however not a keyword in C (and doesn't occur even once in the draft for the C23 standard) but an extension supported only by some compilers, for example clang and icx.
Linux man
页使用_Nullable
来向读者指示它们被允许传递NULL
指针,但头文件中的实际声明不是_Nullable
.
希望向编译器提供静态分析和优化提示_Nullable
和_Nonnull
(指示指针not可以是NULL
)的库将视所使用的平台/编译器而 Select 性地将指针声明为_Nullable
和_Nonnull
.
示例:在Linux上的zipconf.h
(针对libzip
的特定于平台的包含文件)中,您将看到
#define _Nullable
#define _Nonnull
and all the zip
functions will have both _Nullable
and _Nonnull
in their declarations. On Linux these will however be replaced with nothing thanks to the above #define
s. On a platform dominated by clang or icx these definitions will not exist and the implementation specific keywords will then be passed on to the compiler as-is.