What is _Nullable pointer in C?

What is _Nullable pointer in C?


6

What does _Nullable mean in following declaration?

void foo(int *_Nullable ptr)

gcc of version 11.4 doesn’t compile that code, because it treats _Nullable keyword as an argument name. Yet I see this keyword in man 7 pages:
https://man7.org/linux/man-pages/man2/epoll_ctl.2.html
https://man7.org/linux/man-pages/man2/timer_gettime.2.html

Godbolt

New contributor

Ержанов Жалгас is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

0

1 Answer
1


8

_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 and .

The Linux man pages uses _Nullable to indicate to the readers that they are allowed to pass in a NULL pointer, but the actual declarations in the header files are free from _Nullable.


Libraries who wishes to give the compiler the static analysis and optimizing hints _Nullable and _Nonnull (indicating that the pointer may not be NULL) will optionally declare pointers as _Nullable and _Nonnull depending on which platform/compiler that is used.

Example: In zipconf.h (a platform specific include file for libzip) on Linux, you would see

#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 #defines. On a platform dominated by or these definitions will not exist and the implementation specific keywords will then be passed on to the compiler as-is.

2

  • 1

    It is used by warning system. If it is nullable and you do not check for NULL then emit warning.

    – 0___________

    9 hours ago

  • @0___________ I was just thinking, "should I add something about static analysis?" when you commented 🙂 Done!

    – Ted Lyngmo

    9 hours ago




Leave a Reply

Your email address will not be published. Required fields are marked *