"Should I use NULL or 0?
In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
If you have to name the null pointer, call it nullptr; that's what it's going to be called in C++0x. Then, "nullptr" will be a keyword."
This is expecting a pointer. A NULL sentinel attribute should be set to catch this (if you are using GCC). And GCC does catch this exact error.
void blah(int a, ...) __attribute__((sentinel));
blah(123, 0); will cause gcc to spew "warning: missing sentinel in function call" while blah(123, NULL); will not.
Additionally, one should remember types are important in va_arg functions. For example, passing in 0 when a parameter expects a int64_t, is the same error. It is useful to know that float is promoted to double in va_arg functions and int8_t, int16_t, and bool are promoted to int32_t.
This often causes problems with people new to C using GObject's g_object_set() and g_object_get() functions.
3 comments
[ 2.9 ms ] story [ 18.8 ms ] thread"Should I use NULL or 0? In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days. If you have to name the null pointer, call it nullptr; that's what it's going to be called in C++0x. Then, "nullptr" will be a keyword."
Sounds like compiler/standard library in use bug.
void blah(int a, ...) __attribute__((sentinel));
blah(123, 0); will cause gcc to spew "warning: missing sentinel in function call" while blah(123, NULL); will not.
Additionally, one should remember types are important in va_arg functions. For example, passing in 0 when a parameter expects a int64_t, is the same error. It is useful to know that float is promoted to double in va_arg functions and int8_t, int16_t, and bool are promoted to int32_t.
This often causes problems with people new to C using GObject's g_object_set() and g_object_get() functions.