FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Null_pointer

Null pointer

In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value indicating that the pointer or reference does not refer to an object. Programs routinely use null pointers 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.

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 an object. However, in general, most languages do not offer such a guarantee for uninitialized pointers. It might compare equal to other, valid pointers; or it might compare equal to null pointers. It might do both at different times, or the comparison might be undefined behavior. Also, in languages offering such support, the correct use depends on the individual experience of each developer and linter tools. Even when used properly, null pointers are semantically incomplete, since they do not offer the possibility to express the difference between "not applicable", "not currently known", and "not yet determined" values.

In systems 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.

Because a null pointer does not point to a meaningful object, an attempt to access the data stored at that (invalid) memory location may cause a run-time error or immediate program crash. This is the null pointer error, or null pointer exception. It is one of the most common types of software weaknesses, and Tony Hoare, who introduced the concept, has referred to it as a "billion dollar mistake".

#define NULL ((void*)0), the integer value nullptr which is of type nullptr and type nullptr_t were later introduced to C in C23.

Other languages

edit

Programming languages use different literals for the null pointer. In Java and C#, the literal void reference. In Rust, the absence of a value is denoted as try/EAccessViolation exception instance if the null) causes a System.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 0, depending on the type.
  • In Rust, dereferencing a null pointer (which can be received from std::ptr::null<T>(), which is of type "CWE-476: NULL Pointer Dereference". MITRE.
  • 1 2 Lattner, Chris (2011-05-13). "What Every C Programmer Should Know About Undefined Behavior #1/3". blog.llvm.org. Archived from the original on 2023-06-14. Retrieved 2023-06-14.
  • ↑ The const qualifier (§5.4) prevents accidental redefinition of NULL and ensures that NULL can be used where a constant is required."}},"i":0}}]}'/>Stroustrup, Bjarne (March 2001). "Chapter 5:
    The const qualifier (§5.4) prevents accidental redefinition of NULL and ensures that NULL can be used where a constant is required.". The C++ Programming Language (14th printing of 3rd ed.). United States and Canada: Addison–Wesley. p. 88. ISBN 0-201-88954-4.
  • ↑ "The Objective-C 2.0 Programming Language". section "Sending Messages to nil".
  • ↑ "Fearless Security: Memory Safety". Archived from the original on 8 November 2020. Retrieved 4 November 2020.
  • ↑ Contieri, Maxi (2025-06-18). "Code Smell 304 - Null Pointer Exception". Maximiliano Contieri - Software Design. Retrieved 2025-06-18.
  • References

    edit
    "},"url":{"wt":"http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf"},"type":{"wt":"Committee Draft"}},"i":1}},"\n",{"template":{"target":{"wt":"refend","href":"./Template:Refend"},"params":{},"i":2}}]}'>
    View original on Wikipedia | Back | FazBrowse Home