Pointer (computer Programming) - Null Pointer

A null pointer has a value reserved for indicating that the pointer does not refer to a valid object. Null pointers are routinely used to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type.

Null pointers are often considered similar to null values in relational databases, but they have somewhat different semantics. Null pointer in most programming languages means "no value", while null value in relational database means "unknown value". This leads to important difference in practice: two null pointers are considered equal in most programming languages, but two null values in relational database are not (since they represent unknown values, it is unknown whether they are equal).

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. This is known as the nil vector.

In C, two null pointers of any type are guaranteed to compare equal. The macro NULL is defined as an implementation-defined null pointer constant, which in C99 can be portably expressed as the integer value 0 converted implicitly or explicitly to the type void*.

Dereferencing the NULL pointer typically results in an attempted read or write from memory that is not mapped - triggering segmentation fault or access violation. This may represent itself to the developer as a program crash, or be transformed into an exception that can be caught. There are, however, certainly circumstances where this is not the case. For example, in x86-real mode, the address 0000:0000 is readable and usually writable, hence dereferencing the null pointer is a perfectly valid but typically unwanted action that may lead to undefined but non-crashing behaviour in the application. Note also that there are occasions when dereferencing the NULL 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.

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. However, C++11 has introduced an explicit nullptr constant to be used instead.

A null pointer should not be confused with an uninitialized pointer: A null pointer is guaranteed to compare unequal to any pointer that points to a valid object. However, depending on the language and implementation, an uninitialized pointer has either an indeterminate (random or meaningless) value or a specific value that is not necessarily any kind of null pointer constant.

The null reference was invented by C.A.R. Hoare in 1965 as part of the Algol W language. Hoare later (2009) described his invention as a "billion-dollar mistake":

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

Because a null pointer does not point to a meaningful object, an attempt to dereference a null pointer usually (but not always) causes a run-time error or immediate program crash.

  • In C, the behavior of dereferencing a null pointer is undefined. Many implementations cause such code to result in the program being halted with a segmentation fault, because the null pointer representation is chosen to be an address that is never allocated by the system for storing objects. However, this behavior is not universal.
  • In Java, access to a null reference triggers a NullPointerException, which can be caught by error handling code, but the preferred practice is to ensure that such exceptions never occur.
  • In .NET, access to null reference triggers a NullReferenceException to be thrown. Although catching these is generally considered bad practice, this exception type can be caught and handled by the program.
  • In Objective-C, messages may be sent to a nil object (which is essentially a null pointer) without causing the program to be interrupted; the message is simply ignored, and the return value (if any) is nil or 0, depending on the type.

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.

Read more about this topic:  Pointer (computer Programming)

Famous quotes containing the words null and/or pointer:

    A strong person makes the law and custom null before his own will.
    Ralph Waldo Emerson (1803–1882)

    The hardiest skeptic who has seen a horse broken, a pointer trained, or has visited a menagerie or the exhibition of the Industrious Fleas, will not deny the validity of education. “A boy,” says Plato, “is the most vicious of all beasts;” and in the same spirit the old English poet Gascoigne says, “A boy is better unborn than untaught.”
    Ralph Waldo Emerson (1803–1882)