In languages with a tagged architecture, a possibly null pointer can be replaced with a tagged union which enforces explicit handling of the exceptional case in fact, a possibly null pointer can be seen as a tagged pointer with a computed tag. In some programming language environments (at least one proprietary Lisp implementation, for example), the value used as the null pointer (called nil in Lisp) may actually be a pointer to a block of internal data useful to the implementation (but not explicitly reachable from user programs), thus allowing the same register to be used as a useful constant and a quick way of accessing implementation internals. However, C++11 has introduced the explicit null pointer constant nullptr to be used instead. In C++, while the NULL macro was inherited from C, the integer literal for zero has been traditionally preferred to represent a null pointer constant. It is also possible for the compiler to optimize away the null pointer dereference, avoiding a segmentation fault but causing other undesired behavior. There are occasions when dereferencing the pointer to address zero is intentional and well-defined for example, BIOS code written in C for 16-bit real-mode x86 devices may write the IDT at physical address 0 of the machine by dereferencing a null pointer for writing. For example, in x86 real mode, the address 0000:0000 is readable and also usually writable, and dereferencing a pointer to that address is a perfectly valid but typically unwanted action that may lead to undefined but non-crashing behavior in the application. There are, however, certain circumstances where this is not the case. This may manifest itself as a program crash, or be transformed into a software exception that can be caught by program code. In practice, dereferencing a null pointer may result in an attempted read or write from memory that is not mapped, triggering a segmentation fault or memory access violation. Dereferencing a null pointer is undefined behavior in C, and a conforming implementation is allowed to assume that any pointer that is dereferenced is not null. The C standard does not say that the null pointer is the same as the pointer to memory address 0, though that may be the case in practice. The preprocessor macro NULL is defined as an implementation-defined null pointer constant, which in C99 can be portably expressed as ((void *)0) which means that the integer value 0 converted to the type void* (pointer to void). In C, two null pointers of any type are guaranteed to compare equal.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |