11 ms·
Dereferencing null is undefined behavior in a C program - which is another way of saying it is considered an error, and there are no guarantees about what will
by scott_s 11y ago
Dereferencing null is undefined behavior in a C program - which is another way of saying it is considered an error, and there are no guarantees about what will happen as a result of it.
From the standard (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), "If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined." In a footnote, it goes on to define what invalid values are. "Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime."
And null is indeed defined to be 0. Also from the standard: "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function."
- dragontamer 11y ago> Dereferencing null is undefined behavior in a C program - which is another way of saying it is considered an error, and there are no guarantees about what will happen as a result of it. Indeed. It is undefined within C. It is an abstraction that C builds on top of. Dereferencing 0 on the vast majority of embedded systems (or kernel-level code) is very well defined. This is just one of the areas where C has a "leaky abstraction".