12 ms·
If you have ever written a function that accepts a pointer to a single value as an argument, with null pointer not being valid input, then declaring it as a sta
by int_19h 8y ago
If you have ever written a function that accepts a pointer to a single value as an argument, with null pointer not being valid input, then declaring it as a static 1-element array should make things just a little bit better (mainly because the compiler can assume no null).
- jefftk 8y agoYou could define a macro NOTNULL for this, to make it clearer to readers. void foo(char bar[static 1]) vs void foo(NOTNULL(char* bar)) Though I'm not sure whether this can be expressed as a macro, since you need to remove the "*"?
- saagarjha 8y agoHow about this: #define NOTNULL void foo(NOTNULL char *bar) If you're using Objective-C, then nonnull is your friend.
- int_19h 8y agoThe point is to get [static 1] in the expansion of the macro, so that the compiler is aware too, not just the coder.
- saagarjha 8y agoAh. I was wondering why this was difficult; thanks for the clarification!
- 1over137 8y agoThe clang nullability annotations can be used in C/C++ as extensions: https://clang.llvm.org/docs/AttributeReference.html#nullability-attributes https://clang.llvm.org/docs/AttributeReference.html#nullabil... You could then conditionally define your NOTNULL using them or not. The compiler will then complain about incorrect usage.
- shaklee3 8y agoSee here: https://news.ycombinator.com/item?id=18787595 https://news.ycombinator.com/item?id=18787595 The compiler didn't complain when passed a pointer to NULL (ok, not the same as passing NULL.