Any pointer type with the value 0 is called a null pointer. Here is the explanation from the C standard §6.3.2.3:

  1. 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
Answer from medalib on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
To initialize a pointer variable when that pointer variable hasn't been assigned any valid memory address yet. To check for a null pointer before accessing any pointer variable.
Published   January 10, 2025
Top answer
1 of 5
4
The C++ Standard doesn't care. From Section 3.27: Undefined behavior may be expected when this document omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. The dereferencing operators * and -> are only defined for the first type of pointer in the standard - a pointer to an object or function. Everything else is undefined, and is up to how the compiler and runtime environment are implemented. That way lies dragons. On my system: Linux doesn't care, either. Either the process is allowed to access a particular address in memory, or it isn't. If I dereference the null pointer, I get a segfault, just like if I try to dereference a pointer containing 0xdeadbeef. Null isn't special other than that the OS will never allow you to access memory with that address and that it's frequently used as a pointer value that signifies nonexistence.
2 of 5
4
The difference is that NullPointerExceptions come from managed languages which actually check if you're dereferencing NULL, whereas a segfault is the OS telling you to go away and stop messing with memory you shouldn't be messing with. It's really a matter of how much abstraction there is between the language and the underlying architecture (i.e. how low/high level the language is). In a language like Java you can basically imagine every method call/field access looks like this C++ code (and it shares the associated performance cost!): if (!obj) throw NullPointerException(blahblah); obj->method();
Discussions

what is a null pointer in c - Stack Overflow
If you try to read or write from null pointer you will get runtime error which is sometimes called segmentation fault, or null pointer exception. More on stackoverflow.com
🌐 stackoverflow.com
How can you handle a "null pointer" exception to prevent crashes in your C program?
How can you handle a "null pointer" exception to prevent crashes in your C program? More on mindstick.com
🌐 mindstick.com
0
August 16, 2023
c++ - How to catch the null pointer exception? - Stack Overflow
For those looking for the wrath inducing /Eha option, in VS 2019 it's under properties->Configuration properties->C/C++->Code Generation->Enable C++ Exceptions ... There's no such thing as "null pointer exception" in C++. The only exceptions you can catch, is the exceptions explicitly thrown ... More on stackoverflow.com
🌐 stackoverflow.com
c - What is "null pointer assignment error"? - Stack Overflow
If you try, you will get a runtime exception (access violation, segmentation fault, etc.). ... Sign up to request clarification or add additional context in comments. ... I actually can not recall the source, but according to the source, this run time error is restricted to small and medium memory models being put into use by corresponding compiler. You see, as told before, the null pointer ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c programming tutorial › null pointer in c
Null pointer in C | How Null pointer work in C with Examples
March 28, 2023 - So usually when we try to write or read from a null pointer we get run time error as we saw in the above code which we get segmentation fault which is a null pointer exception sometimes it also throws an exception as null pointer exception.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
MindStick
mindstick.com › forum › 159520 › how-can-you-handle-a-null-pointer-exception-to-prevent-crashes-in-your-c-program
How can you handle a "null pointer" exception to prevent crashes in your C program? – MindStick
August 16, 2023 - Use a try-catch block. The try-catch block is a way to handle errors in C. The try block contains the code that might cause an error, and the catch block handles the error. Here is an example of how to use a try-catch block to handle a null pointer exception:
🌐
C2
wiki.c2.com
Null Pointer Exception
This site uses features not available in older browsers
🌐
Unstop
unstop.com › home › blog › null pointer in c | a detailed explanation with examples
Null Pointer In C | A Detailed Explanation With Examples
May 3, 2024 - Error Signaling: Null pointers in C are commonly used to signal error conditions or exceptional cases, particularly in functions that perform operations like dynamic memory allocation (malloc(), calloc(), etc.).
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Null_pointer
Null pointer - Wikipedia
2 days ago - 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.
🌐
Quora
quora.com › What-is-a-null-pointer-assignment-error-in-C-programming-What-is-a-way-to-debug-it
What is a null pointer assignment error in C programming? What is a way to debug it? - Quora
Null pointer errors are usually the result of one or more programmer assumptions being violated. Most null pointer issues result in general software reliability problems, but if an att...
🌐
Cristianadam
cristianadam.eu › 20160914 › nullpointerexception-in-c-plus-plus
NullPointerException in C++ - Cristian Adam
Message: write to nullptr ~Message: write to nullptr OS exception: null pointer! ------------------------------------0 · For brevity I displayed only the first block. How should except::register_for_os_exceptions() look like? Can it be done in a cross-platform way, or only with platform specific ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_null_pointer.htm
NULL Pointer in C
The NULL constant is defined in the header files stdio.h, stddef.h as well as stdlib.h. A pointer is initialized to NULL to avoid the unpredicted behavior of a program or to prevent segmentation fault errors.
🌐
Dobegin
dobegin.com › npe-hell
Null pointer exceptions hell - do begin
Null pointer exception (NPE) is known by different names in different programming languages. Null pointer dereference in C/C++, NullPointerException in Java, NullReferenceException in C# and .NET, and many other names in scripting languages like JavaScript’s “undefined is not a function” among them.
🌐
Quora
quora.com › What-happens-when-we-try-to-access-a-null-pointer-in-C
What happens when we try to access a null pointer in C? - Quora
Answer (1 of 4): The standard says that accessing a NULL ptr is “undefined behavior”. Undefined behavior can be anything, including: * Nothing at all - continue running the program as if nothing happened * Crashing the application * Corrupting application data From Wikipedia we have this: “I...
Top answer
1 of 12
75

There's no such thing as "null pointer exception" in C++. The only exceptions you can catch, is the exceptions explicitly thrown by throw expressions (plus, as Pavel noted, some standard C++ exceptions thrown intrinsically by standard operator new, dynamic_cast etc). There are no other exceptions in C++. Dereferencing null pointers, division by zero etc. does not generate exceptions in C++, it produces undefined behavior. If you want exceptions thrown in cases like that it is your own responsibility to manually detect these conditions and do throw explicitly. That's how it works in C++.

Whatever else you seem to be looking for has noting to do with C++ language, but rather a feature of particular implementation. In Visual C++, for example, system/hardware exceptions can be "converted" into C++ exceptions, but there's a price attached to this non-standard functionality, which is not normally worth paying.

2 of 12
30

You cannot. De-referencing a null-pointer is a system thing.

On Linux, the OS raises signals in your application. Take a look at csignal to see how to handle signals. To "catch" one, you'd hook a function in that will be called in the case of SIGSEGV. Here you could try to print some information before you gracefully terminate the program.

Windows uses structured-exception-handling. You could use the instristics __try/__except, as outlined in the previous link. The way I did it in a certain debug utility I wrote was with the function _set_se_translator (because it closely matches hooks). In Visual Studio, make sure you have SEH enabled. With that function, you can hook in a function to call when the system raises an exception in your application; in your case it would call it with EXCEPTION_ACCESS_VIOLATION. You can then throw an exception and have it propagate back out as if an exception was thrown in the first place.

🌐
Javatpoint
javatpoint.com › null-pointer-in-c
Null Pointer in C - javatpoint
Null Pointer in C with programming examples for beginners and professionals covering concepts, control statements, c array, c pointers, c structures, c union, c strings and more.
Top answer
1 of 5
20

http://www.faqs.org/qa/qa-3786.html

A NULL pointer assignment is a runtime error It occurs due to various reasons one is that your program has tried to access an illegal memory location. Illegal location means either the location is in the operating systems address space or in the other processes memory space. In stdio.h NULL is defined as 0 So whenever your program tries to access 0th location the operating system kills your program with runtime assignment error because the 0th location is in the operating systems address space and operating system doesn't allow access to its address space by user program .

Example code:

int* ptr = NULL;  
*ptr = 3;

Explanation:
On almost every system, address 0 is reserved. System won't allow you to write to that location. If you try, you will get a runtime exception (access violation, segmentation fault, etc.).

2 of 5
2

I actually can not recall the source, but according to the source, this run time error is restricted to small and medium memory models being put into use by corresponding compiler. You see, as told before, the null pointer actually does not points to zero, in fact different compilers use different but fixed memory location to be used as null pointer.

Lets consider the case of TC compiler, this compiler places four zero bytes at the bottom of the data segment and TC copyright notice. TC also uses DS:0000 location, bottom of the data segment as null pointers location. So, assigning a value to this null pointer, would actully change the four bytes and probably, mess up the copyright notice.

Now, at the program termination, the four zeros and copyright banner are checked for any kind of alteration. If any alterations are found, it generates a Null Pointer Assignment error.

So, I think its not just the null pointer, any pointer that gets wild, if tries to access some key areas, you are greeted with Null Pointer Assignment Error.

🌐
Udemy
blog.udemy.com › home › understanding null pointer exception
Understanding Null Pointer Exception - Udemy Blog
December 4, 2019 - To call the methods of that object ... Null pointer exception occurs when a reference variable of the object contains a null value and that object is used for calling methods or instance variables....
🌐
Learnsic
learnsic.com › blog › understanding-null-pointer-exception
Understanding Null Pointer Exception
March 29, 2024 - This website uses cookies to ensure you get the best experience & by continuing to use our website, you agree to our Privacy and Cookie Policy.
🌐
Quora
quora.com › What-is-a-Null-Pointer-Exception-in-C
What is a Null Pointer Exception in C++? - Quora
Answer (1 of 2): It is not called that, but what happens if you screw up is that you fail to attach storage to a pointer and then try to store or read from it. Additionally you can have the wrong address in a pointer and then try to access it ...