I saw a Video about modern C programming, and one topic was that when you use [static 1]
with a parameter, it indicates to the reader and the compiler that a pointer should never be NULL.
You would use it like this:
void foo(int my_ptr[static 1]) { // my_ptr should never be NULL}
instead of:
void foo(int* my_ptr) { // my_ptr can be NULL}
If you enable -Wnonnull
, it even throws a warning when you pass NULL to the first option.My question is, why is this not common practice and used everywhere?
I don't think this can or should replace NULL checks, but it certainly would be useful, nevertheless, IMO.
I also heard that compilers might remove NULL checks if they see that the pointer should never be NULL because of the [static 1]
, which would be bad, but I don't know if this is true.