<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Null_pointer</id>
	<title>Null pointer - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Null_pointer"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Null_pointer&amp;action=history"/>
	<updated>2026-06-23T00:36:15Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Null_pointer&amp;diff=679894&amp;oldid=prev</id>
		<title>imported&gt;Ptrnext: +wl; move ref</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Null_pointer&amp;diff=679894&amp;oldid=prev"/>
		<updated>2025-11-18T08:11:35Z</updated>

		<summary type="html">&lt;p&gt;+wl; move ref&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Value indicating that a referenced dataset is invalid or doesn&amp;#039;t exist}}&lt;br /&gt;
&lt;br /&gt;
In [[computing]], a &amp;#039;&amp;#039;&amp;#039;null pointer&amp;#039;&amp;#039;&amp;#039; (sometimes shortened to &amp;#039;&amp;#039;&amp;#039;nullptr&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;null&amp;#039;&amp;#039;&amp;#039;) or &amp;#039;&amp;#039;&amp;#039;null reference&amp;#039;&amp;#039;&amp;#039; is a value saved for indicating that the [[Pointer (computer programming)|pointer]] or [[reference (computer science)|reference]] does not refer to a valid [[Object (computer science)|object]]. Programs routinely use null pointers to represent conditions such as the end of a [[List (computing)|list]] of unknown length or the failure to perform some action; this use of null pointers can be compared to [[nullable type]]s and to the &amp;#039;&amp;#039;Nothing&amp;#039;&amp;#039; value in an [[option type]].&lt;br /&gt;
&lt;br /&gt;
A null pointer should not be confused with an [[uninitialized variable|uninitialized pointer]]: a null pointer is guaranteed to compare unequal to any pointer that points to a valid object. However, in general, most languages do not offer such 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 [[semantic completeness|semantically incomplete]], since they do not offer the possibility to express the difference between &amp;quot;not applicable&amp;quot;, &amp;quot;not known&amp;quot;, and &amp;quot;future&amp;quot; values.{{cn|date=March 2025}}&lt;br /&gt;
&lt;br /&gt;
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 &amp;#039;&amp;#039;&amp;#039;null pointer error&amp;#039;&amp;#039;&amp;#039;, or &amp;#039;&amp;#039;&amp;#039;null pointer exception&amp;#039;&amp;#039;&amp;#039;. It is one of the most common types of software weaknesses,&amp;lt;ref&amp;gt;{{cite web |url=https://cwe.mitre.org/data/definitions/476.html |title=CWE-476: NULL Pointer Dereference |website=MITRE}}&amp;lt;/ref&amp;gt; and [[Tony Hoare]], who introduced the concept, has referred to it as a &amp;quot;billion dollar mistake&amp;quot;.&amp;lt;ref name=&amp;quot;:0&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== C ==&lt;br /&gt;
&lt;br /&gt;
In [[C (programming language)|C]], two null pointers of any type are guaranteed to compare equal.&amp;lt;ref&amp;gt;[[#c-std|ISO/IEC 9899]], clause 6.3.2.3, paragraph 4.&amp;lt;/ref&amp;gt; Prior to C23, the preprocessor macro &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; was provided, defined as an implementation-defined null pointer constant in &amp;lt;code&amp;gt;&amp;lt;[[stdlib.h]]&amp;gt;&amp;lt;/code&amp;gt;,&amp;lt;ref&amp;gt;[[#c-std|ISO/IEC 9899]], clause 7.17, paragraph 3: &amp;#039;&amp;#039;NULL... which expands to an implementation-defined null pointer constant...&amp;#039;&amp;#039;&amp;lt;/ref&amp;gt; which in [[C99]] can be portably expressed with {{code|#define NULL ((void*)0)|c}}, the integer value &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; [[Type conversion|converted]] to the type &amp;lt;code&amp;gt;void*&amp;lt;/code&amp;gt; (see [[pointer to void type]]).&amp;lt;ref&amp;gt;[[#c-std|ISO/IEC 9899]], clause 6.3.2.3, paragraph 3.&amp;lt;/ref&amp;gt; Since [[C23 (C standard revision)|C23]], a null pointer is represented with &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt; which is of type &amp;lt;code&amp;gt;nullptr_t&amp;lt;/code&amp;gt; (first introduced to C++11), providing a type safe null pointer.&amp;lt;ref name=&amp;quot;N3042&amp;quot;&amp;gt;{{cite web |title=WR14-N3042: Introduce the nullptr constant |url=https://open-std.org/JTC1/SC22/WG14/www/docs/n3042.htm |website=open-std.org |archive-url=https://web.archive.org/web/20221224043228/https://open-std.org/JTC1/SC22/WG14/www/docs/n3042.htm |archive-date=December 24, 2022 |date=2022-07-22 |url-status=live}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The C standard does not say that the null pointer is the same as the pointer to [[memory address]]&amp;amp;nbsp;0, though that may be the case in practice. [[Dereferencing]] a null pointer is [[undefined behavior]] in C,&amp;lt;ref name=&amp;quot;ub&amp;quot;/&amp;gt; and a conforming implementation is allowed to assume that any pointer that is dereferenced is not null.&lt;br /&gt;
&lt;br /&gt;
In practice, dereferencing a null pointer may result in an attempted read or write from [[computer memory|memory]] that is not mapped, triggering a [[segmentation fault]] or memory access violation. This may manifest itself as a program crash, or be transformed into a software [[exception handling|exception]] that can be caught by program code. There are, however, certain circumstances where this is not the case. For example, in [[Intel x86|x86]] [[real mode]], the address &amp;lt;code&amp;gt;0000:0000&amp;lt;/code&amp;gt; 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; if a null pointer is represented as a pointer to that address, dereferencing it will lead to that behavior. There are occasions when dereferencing a pointer to address zero &amp;#039;&amp;#039;is&amp;#039;&amp;#039; intentional and well-defined; for example, [[BIOS]] code written in C for 16-bit real-mode x86 devices may write the [[interrupt descriptor table]] (IDT) at physical address&amp;amp;nbsp;0 of the machine by dereferencing a pointer with the same value as a null pointer for writing. It is also possible for the compiler to optimize away the null pointer dereference, avoiding a segmentation fault but causing other undesired behavior.&amp;lt;ref&amp;gt;{{Cite web |last=Lattner |first=Chris |date=2011-05-13 |title=What Every C Programmer Should Know About Undefined Behavior #1/3 |url=https://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html |url-status=live |archive-url=https://web.archive.org/web/20230614124121/https://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html |archive-date=2023-06-14 |access-date=2023-06-14 |website=blog.llvm.org |language=en}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
In C++, while the &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; macro was inherited from C, the integer literal for zero has been traditionally preferred to represent a null pointer constant.&amp;lt;ref&amp;gt;{{cite book |last1=Stroustrup |first1=Bjarne |authorlink1=Bjarne Stroustrup |title=[[The C++ Programming Language]] |edition=14th printing of 3rd |date=March 2001 |publisher=Addison–Wesley |location=United States and Canada |isbn=0-201-88954-4 |page=[https://archive.org/details/cprogramminglang00stro_0/page/88 88] |chapter=Chapter 5: &amp;lt;br /&amp;gt;The &amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt; qualifier (§5.4) prevents accidental redefinition of &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; and ensures that &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; can be used where a constant is required. }}&amp;lt;/ref&amp;gt; However, [[C++11]] introduced the explicit null pointer constant &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;nullptr_t&amp;lt;/code&amp;gt; to be used instead, providing a type safe null pointer. &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;nullptr_t&amp;lt;/code&amp;gt; were later introduced to C in C23.&lt;br /&gt;
&lt;br /&gt;
== Other languages ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Programming languages use different literals for the &amp;#039;&amp;#039;null pointer&amp;#039;&amp;#039;. In [[Java (programming language)|Java]] and [[C Sharp (programming language)|C#]], the literal &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; is provided as a literal for reference types. In [[Pascal (programming language)|Pascal]] and [[Swift (programming language)|Swift]], a null pointer is called &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. In [[Eiffel (programming language)|Eiffel]], it is called a &amp;lt;code&amp;gt;void&amp;lt;/code&amp;gt; reference. In [[Rust (programming language)|Rust]], the absence of a value is denoted as &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, but a true null pointer is &amp;lt;code&amp;gt;std::ptr::null()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Null dereferencing ==&lt;br /&gt;
&lt;br /&gt;
Because a null pointer does not point to a meaningful object, an attempt to [[dereference]] (i.e., access the data stored at that memory location) a null pointer usually (but not always) causes a run-time error or immediate program crash. [[MITRE]] lists the null pointer error as one of the most commonly exploited software weaknesses.&amp;lt;ref&amp;gt;{{cite web |url=https://cwe.mitre.org/data/definitions/476.html |title=CWE-476: NULL Pointer Dereference |website=MITRE}}&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* In [[C (programming language)|C]], dereferencing a null pointer is [[undefined behavior]].&amp;lt;ref name=&amp;quot;ub&amp;quot;&amp;gt;[[#c-std|ISO/IEC 9899]], clause 6.5.3.2, paragraph 4, esp. footnote 87.&amp;lt;/ref&amp;gt; Many implementations cause such code to result in the program being halted with an [[access violation]], 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. It is also not guaranteed, since compilers are permitted to optimize programs under the assumption that they are free of undefined behavior. This behaviour is the same in [[C++]], as there is no null pointer exception in the C++ language. On platforms such as [[Unix-like]] systems and [[Windows]] with the [[Visual Studio]] compiler, an access violation causes a C/C++ &amp;lt;code&amp;gt;[[SIGSEGV]]&amp;lt;/code&amp;gt; signal to be issued. Although in C/C++ null dereferences are not exceptions which can be caught in C++ &amp;lt;code&amp;gt;try&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;catch&amp;lt;/code&amp;gt; blocks, it is possible to &amp;quot;catch&amp;quot; such an access violation by using (&amp;lt;code&amp;gt;std::&amp;lt;/code&amp;gt;)&amp;lt;code&amp;gt;signal()&amp;lt;/code&amp;gt; in C/C++ to specify a handler to be called when that signal is issued.&lt;br /&gt;
** Some external C++ libraries, such as [[POCO C++ Libraries]], include a &amp;lt;code&amp;gt;NullPointerException&amp;lt;/code&amp;gt; class. Unlike Java, where &amp;lt;code&amp;gt;java.lang.NullPointerException&amp;lt;/code&amp;gt; extends &amp;lt;code&amp;gt;java.lang.RuntimeException&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Poco::NullPointerException&amp;lt;/code&amp;gt; instead extends &amp;lt;code&amp;gt;Poco::LogicException&amp;lt;/code&amp;gt;.&amp;lt;ref&amp;gt;{{cite web|title=Class Poco::NullPointerException|url=https://docs.pocoproject.org/current/Poco.NullPointerException.html|access-date=17 August 2025|publisher=docs.pocoproject.org}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* In [[Cyclone (programming language)|Cyclone]], a failed null pointer check will throw a &amp;lt;code&amp;gt;Null_Exception&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In [[D (programming language)|D]], much like C++, a null pointer dereference results in a segmentation fault.&lt;br /&gt;
* In [[Delphi (programming language)|Delphi]] and many other Pascal implementations, the constant {{code|nil}} represents a null pointer to the first address in memory which is also used to initialize managed variables. Dereferencing it raises an external OS exception which is mapped onto a Pascal {{code|EAccessViolation}} exception instance if the {{code|System.SysUtils}} unit is linked in the {{code|uses}} clause.&lt;br /&gt;
* In [[Java (programming language)|Java]], access to a null reference (&amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;) causes a {{Javadoc:SE|java/lang|NullPointerException}} (NPE), which can be caught by error handling code, but the preferred practice is to ensure that such exceptions never occur.&lt;br /&gt;
* In [[.Net (programming language)|.NET]] and [[C Sharp (programming language)|C#]], access to null reference (&amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;) causes a {{code|NullReferenceException}} to be thrown. Although catching these is generally considered bad practice, this exception type can be caught and handled by the program.&lt;br /&gt;
* In [[Objective-C]], messages may be sent to a {{code|nil}} object (which is a null pointer) without causing the program to be interrupted; the message is simply ignored, and the return value (if any) is {{code|nil}} or {{code|0}}, depending on the type.&amp;lt;ref&amp;gt;{{cite web |title=The Objective-C 2.0 Programming Language |url=https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW7 |at=section &amp;quot;Sending Messages to nil&amp;quot;}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* In [[Rust (programming language)|Rust]], dereferencing a null pointer (&amp;lt;code&amp;gt;std::ptr::null()&amp;lt;/code&amp;gt;) in an &amp;lt;code&amp;gt;unsafe&amp;lt;/code&amp;gt; block results in undefined behaviour, which usually results in a segmentation fault or corrupted memory.&lt;br /&gt;
* Before the introduction of [[Supervisor Mode Access Prevention]] (SMAP), a null pointer dereference bug could be exploited by mapping [[Zero page|page zero]] into the attacker&amp;#039;s [[address space]] and hence causing the null pointer to point to that region. This could lead to [[Arbitrary code execution|code execution]] in some cases.&amp;lt;ref&amp;gt;{{cite web |url=https://project-zero.issues.chromium.org/issues/42452311 |title=OS X exploitable kernel NULL pointer dereference in AppleGraphicsDeviceControl}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Mitigation ==&lt;br /&gt;
&lt;br /&gt;
While there could be languages with no nulls, most do have the possibility of nulls so there are techniques to avoid or aid debugging null pointer dereferences.&amp;lt;ref name=&amp;quot;BondNethercote2007&amp;quot;&amp;gt;{{cite book|last1=Bond|first1=Michael D.|last2=Nethercote|first2=Nicholas|last3=Kent|first3=Stephen W.|last4=Guyer|first4=Samuel Z.|last5=McKinley|first5=Kathryn S.|title=Proceedings of the 22nd annual ACM SIGPLAN conference on Object oriented programming systems and applications - OOPSLA &amp;#039;07|chapter=Tracking bad apples|year=2007|pages=405|doi=10.1145/1297027.1297057|isbn=9781595937865|s2cid=2832749}}&amp;lt;/ref&amp;gt; Bond et al. suggest modifying the [[Java virtual machine]] (JVM) to keep track of null propagation.&amp;lt;ref name=&amp;quot;BondNethercote2007&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are three levels of handling null references, in order of effectiveness:&lt;br /&gt;
# languages with no null;&lt;br /&gt;
# languages that can statically analyse code to avoid the possibility of null dereference at run time;&lt;br /&gt;
# if null dereference can occur at runtime, tools that aid debugging.&lt;br /&gt;
&lt;br /&gt;
Pure [[functional languages]] are an example of level 1 since no direct access is provided to pointers and all code and data is immutable. User code running in [[interpreter (computing)|interpreted]] or virtual-machine languages generally does not suffer the problem of null pointer dereferencing.{{dubious|VM claim|date=September 2024}}&lt;br /&gt;
&lt;br /&gt;
Where a language does provide or utilise pointers which could become void, it is possible to avoid runtime null dereferences by providing [[void safety|compilation-time checking]] via [[static analysis]] or other techniques, with syntactic assistance from language features such as those seen in the [[Eiffel programming language]] with Void safety&amp;lt;ref&amp;gt;{{cite web | url=https://www.eiffel.org/doc/eiffel/Void-safety-_Background%2C_definition%2C_and_tools |title=Void-safety: Background, definition, and tools|access-date=2021-11-24 }}&amp;lt;/ref&amp;gt; to avoid null dereferences, [[D programming language|D]],&amp;lt;ref name=&amp;quot;SafeD&amp;quot;&amp;gt;{{cite web |title=SafeD – D Programming Language |url=http://dlang.org/safed.html |author=Bartosz Milewski |access-date=17 July 2014}}&amp;lt;/ref&amp;gt; and [[Rust programming language|Rust]].&amp;lt;ref&amp;gt;{{cite web|title=Fearless Security: Memory Safety|url=https://hacks.mozilla.org/2019/01/fearless-security-memory-safety/|access-date=4 November 2020|archive-date=8 November 2020|archive-url=https://web.archive.org/web/20201108003116/https://hacks.mozilla.org/2019/01/fearless-security-memory-safety/|url-status=live}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages analysis can be performed using external tools, but these are weak compared to direct language support with compiler checks since they are limited by the language definition itself.&lt;br /&gt;
&lt;br /&gt;
The last resort of level 3 is when a null reference occurs at runtime, debugging aids can help.&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
&lt;br /&gt;
In 2009, [[Tony Hoare]] stated&amp;lt;ref name=&amp;quot;:0&amp;quot;&amp;gt;{{cite web&lt;br /&gt;
|url=http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare&lt;br /&gt;
|title=Null References: The Billion Dollar Mistake&lt;br /&gt;
|publisher=InfoQ.com&lt;br /&gt;
|date=2009-08-25&lt;br /&gt;
|author=Tony Hoare&lt;br /&gt;
|author-link=Tony Hoare&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web&lt;br /&gt;
|url=https://qconlondon.com/london-2009/qconlondon.com/london-2009/presentation/Null%2BReferences_%2BThe%2BBillion%2BDollar%2BMistake.html&lt;br /&gt;
|title=Presentation: &amp;quot;Null References: The Billion Dollar Mistake&amp;quot;&lt;br /&gt;
|publisher=InfoQ.com&lt;br /&gt;
|date=2009-08-25&lt;br /&gt;
|author=Tony Hoare&lt;br /&gt;
|author-link=Tony Hoare&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;{{Cite web |last=Contieri |first=Maxi |date=2025-06-18 |title=Code Smell 304 - Null Pointer Exception |url=https://maximilianocontieri.com/code-smell-304-null-pointer-exception |access-date=2025-06-18 |website=Maximiliano Contieri - Software Design |language=en}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
that he invented the null reference in 1965 as part of the [[ALGOL W]] language. In that 2009 reference Hoare describes his invention as a &amp;quot;billion-dollar mistake&amp;quot;:&lt;br /&gt;
{{quote|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&amp;#039;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.}}&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Memory debugger]]&lt;br /&gt;
* [[Zero page]]&lt;br /&gt;
&lt;br /&gt;
== Notes==&lt;br /&gt;
&lt;br /&gt;
{{refs}}&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
{{refbegin}}&lt;br /&gt;
* {{cite book |ref = c-std |author = Joint Technical Committee ISO/IEC JTC 1, Subcommittee SC 22, Working Group WG 14 |title = International Standard ISO/IEC 9899 |date = 2007-09-08&amp;lt;!-- 19:22:32 +0000 --&amp;gt;|url = http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf |type = Committee Draft }}&lt;br /&gt;
{{refend}}&lt;br /&gt;
&lt;br /&gt;
{{Nulls}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Pointers (computer programming)]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Ptrnext</name></author>
	</entry>
</feed>