C++ Annotations
icce.rug.nl
icce.rug.nl
Here's the problem with the macro: the macro NULL can be either 0 or can be (void * ) 0. The issue is that the former can be accidentally interpreted as an integer depending on context.
In both C and C++, this manifests when you pass NULL to a varargs function. Instead of passing in a null pointer, the compiler may instead pass in an integer zero. If pointers and ints have different sizes, hilarity ensues. If the null pointer does not have the same bit representation as int zero, hilarity ensues.
In C++, this happens even more commonly. If there is an operator overload and you pass in NULL, then if NULL is 0 it will chose the int overload, if NULL is (void * ) 0 it will chose a pointer overload.
Never use NULL. Ever.
How can that happen? If I recall the standard, variadic functions have default promotion of non-declared args: integral promotion for integral types, floats to doubles. Why would the compiler want to convert a pointer (which (void*)0 is) to an integer?
In C: the macro NULL is an implementation-defined null pointer constant, which may be
> an integer constant expression with the value 0
> an integer constant expression with the value 0 cast to the type void*
In C++: the macro NULL is an implementation-defined null pointer constant, which may be
> an integral constant expression rvalue of integer type that evaluates to zero (until C++11)
> an integer literal with value zero, or a prvalue of type std::nullptr_t (since C++11)
On most systems, the null pointer happens to be a pointer to address zero. For other systems, e.g. 8086 16-bit mode, you can create a intptr_t (which is the same size as a pointer) and set it to zero. Then cast it to a pointer. Such casting is always done bitwise, so it will work.
The compiler knows to turn the following into the null pointer
void *p = (void *) 0;
but the compiler will NOT turn the following into a null pointer void *p = (void *) (uintptr_t) 0;
because the literal 0 is now in an integer context, not a pointer context.When is that the case? Like, in some specific library/compiler implementations?
For example, I've seen this submission for hours now, and every time I assumed it was sort of function annotation/decorator syntax addition to achieve some interesting result, which didn't pique my interest at those times.
Is any other field quite as bad with this as software development seems to be?
Off the top of my head, I would claim that any large subfield of mathematics is probably as bad, if not worse, than software development (see: all the words used to describe “a collection of things”). I would expect this to be true for any sufficiently developed field of study.
> This document is intended for knowledgeable users of C (or any other language using a C-like grammar, like Perl or Java) who would like to know more about, or make the transition to, C++
No, seen as in "Seen on the front page but it didn't interest me enough at the time to look into it given what I assumed it was about."
[1] he is the writer of the C++ annotations.
[1] https://gitlab.com/fbb-git/cppannotations/commits/master