Transact-SQL
Reinforcement Learning
R Programming
React Native
Python Design Patterns
Python Pillow
Python Turtle
Verbal Ability
Interview Questions
Company Questions
Artificial Intelligence
Cloud Computing
Data Science
Machine Learning
Data Structures
Operating System
Computer Network
Compiler Design
Computer Organization
Discrete Mathematics
Ethical Hacking
Computer Graphics
Software Engineering
Web Technology
Cyber Security
C Programming
Control System
Data Mining
Data Warehouse
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
I am a complete novice to C, and during my university work I've come across comments in code that often refer to de-referencing a NULL pointer. I do have a background in C#, I've been getting by that this might be similar to a "NullReferenceException" that you get in .Net, but now I am having serious doubts.
Can someone please explain to me in layman's terms exactly what this is and why it is bad?
A NULL pointer points to memory that doesn't exist. This may be address 0x00000000 or any other implementation-defined value (as long as it can never be a real address). Dereferencing it means trying to access whatever is pointed to by the pointer. The * operator is the dereferencing operator:
This is exactly the same thing as a NullReferenceException in C#, except that pointers in C can point to any data object, even elements inside an array.
Dereferencing just means accessing the memory value at a given address. So when you have a pointer to something, to dereference the pointer means to read or write the data that the pointer points to.
In C, the unary * operator is the dereferencing operator. If x is a pointer, then *x is what x points to. The unary & operator is the address-of operator. If x is anything, then &x is the address at which x is stored in memory. The * and & operators are inverses of each other: if x is any data, and y is any pointer, then these equations are always true:
A null pointer is a pointer that does not point to any valid data (but it is not the only such pointer). The C standard says that it is undefined behavior to dereference a null pointer. This means that absolutely anything could happen: the program could crash, it could continue working silently, or it could erase your hard drive (although that's rather unlikely).
In most implementations, you will get a "segmentation fault" or "access violation" if you try to do so, which will almost always result in your program being terminated by the operating system. Here's one way a null pointer could be dereferenced:
And yes, dereferencing a null pointer is pretty much exactly like a NullReferenceException in C# (or a NullPointerException in Java), except that the langauge standard is a little more helpful here. In C#, dereferencing a null reference has well-defined behavior: it always throws a NullReferenceException . There's no way that your program could continue working silently or erase your hard drive like in C (unless there's a bug in the language runtime, but again that's incredibly unlikely as well).
basically, almost anything involving (*p) or implicitly involving (*p) , e.g. p->... which is a shorthand for (*p). ... ; except for pointer declaration.
A null pointer has a reserved value, often but not necessarily the value zero, indicating that it refers to no object .. Since a null-valued pointer does not refer to a meaningful object, an attempt to dereference a null pointer usually causes a run-time error.
Quoting from wikipedia :
A pointer references a location in memory, and obtaining the value at the location a pointer refers to is known as dereferencing the pointer.
Dereferencing is done by applying the unary * operator on the pointer.
"Dereferencing a NULL pointer" means performing *p when the p is NULL
A NULL pointer points to memory that doesn't exist, and will raise Segmentation fault . There's an easier way to de-reference a NULL pointer, take a look.
Since 0 is never a valid pointer value, a fault occurs.
Lots of confusion and confused answers here. First of all, there is strictly speaking nothing called a "NULL pointer". There are null pointers, null pointer constants and the NULL macro.
Start by studying my answer from Codidact: What's the difference between null pointers and NULL? Quoting some parts of it here:
There are three different, related concepts that are easy to mix up: null pointers null pointer constants the NULL macro Formal definitions The first two of these terms are formally defined in C17 6.3.2.3/3: An integer constant expression with the value 0 , or such an expression cast to type void * , is called a null pointer constant . 67) 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.
In other words, a null pointer is a pointer of any type pointing at a well-defined "nowhere". Any pointer can turn into a null pointer when it is assigned a null pointer constant.
The standard mentions 0 and (void*)0 as two valid null pointer constants, but note that it says "an integer constant expression with the value 0 ". This means that things like 0u , 0x00 and other variations are also null pointer constants. These are particular special cases that can be assigned to any pointer type, regardless of the various type compatibility rules that would normally apply.
Notably, both object pointers and function pointers can be null pointers. Meaning that we must be able to assign null pointer constants to them, no matter the actual pointer type.
The note 67) from above adds (not normative):
67) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.19.
where 7.19 simply defines NULL as (normative):
NULL which expands to an implementation-defined null pointer constant;
In theory this could perhaps be something other than 0 and (void*)0 , but the implementation-defined part is more likely saying that NULL can either be #define NULL 0 or #define NULL (void*)0 or some other integer constant expression with the value zero, depending on the C library used. But all we need to know and care about is that NULL is a null pointer constant.
NULL is also the preferred null pointer constant to use in C code, because it is self-documenting and unambiguous (unlike 0 ). It should only be used together with pointers and not for any other purpose.
Additionally, do not mix this up with "null termination of strings", which is an entirely separate topic. Null termination of strings is just a value zero, often referred to either as nul (one L) or '\0' (an octal escape sequence), just to separate it from null pointers and NULL .
Dereferencing
Having cleared that out, we cannot access what a null pointer points at, because it is as mentioned a well-defined "nowhere". The process of accessing what a pointer points at is known as dereferencing , and is done in C (and C++) through the unary * indirection operator. The C standard specifying how this operator works simply states (C17 6.5.3.3):
If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined
Where an informative note adds:
Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime.
And this would be where "segmentation faults" or "null pointer/reference exceptions" might be thrown. The reason for such is almost always an application bug such as these examples:
Let's look at an example of dereferencing a NULL pointer, and talk about it.
Here is an example of dereferencing a NULL pointer, from this duplicate question here: uint32_t *ptr = NULL; :
Memory hasn't been allocated for the uint32_t , so calling *ptr , which "dereferences" the pointer, ptr , or otherwise said: accesses memory at an unallocated ( NULL --usually 0 , but implementation-defined) address, is illegal. It is "undefined behavior"--ie: a bug.
So, you should statically (preferred, where possible), or dynamically allocate space for a uint32_t and then only dereference a pointer which points to valid memory, as follows.
Here is how to statically allocate memory and use it with a pointer. Note even that the memory for the pointer itself is statically allocated in my example!:
Note, dynamic allocation is fine too, but static memory allocation is safer, deterministic, faster, better for memory-constrained embedded systems, blah blah blah. The point is simply that you cannot legally dereference any pointer (meaning: put an asterisk "dereference operator" in front of it, like *ptr ) which does not point to a chunk of allocated memory. I generally allocate memory statically by declaring a variable.
A null pointer is basically a pointer which points to no object in the memory.
Dereferencing a pointer means trying to access object at the pointer's memory address.(Pointer is a reference and trying to de- reference means you are trying to get the actual object it points to ).
When you try to dereference a null pointer it means you are trying to address an object that isn't there . This leads to undefined behaviour .
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
IMAGES
VIDEO
COMMENTS
A null pointer assignment error, or many other errors, can be assigned to this issue and example. In simpler architecture or programming environments, It can refer to any code which unintentionally ends up creating nulls as pointers, or creates a bug that in anyway halts the execution, like overwriting a byte in the return stack, overwriting ...
NULL Pointer in C
12.8 — Null pointers. Alex January 10, 2024. In the previous lesson (12.7 -- Introduction to pointers), we covered the basics of pointers, which are objects that hold the address of another object. This address can be dereferenced using the dereference operator (*) to get the object at that address: #include <iostream> int main() { int x { 5 };
NULL Pointers. A NULL pointer is a pointer that lacks a reference to a valid memory location. It's typically used to indicate that a pointer doesn't have a specific memory address assigned, often serving as a placeholder or default value for pointers. Here's a code example that demonstrates the use of a NULL pointer:
Dangling, Void , Null and Wild Pointers in C
NULL Pointer in C. A NULL pointer in C is a pointer that doesn't point to any of the memory locations. 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.
1. NULL should not be used to refer to a null character; it's a null pointer constant. C implementations can define NULL either as ((void*)0) the outer parentheses are necessary) or as 0, or in any of several other forms. C++, which is a different language, has different requirements for null pointer constants, and C++ implementations commonly ...
Null pointer in C | How Null pointer work in C with Examples
C++ Null Pointers
NULL is itself a special type of pointer. When we initialize a pointer with NULL then that pointer also becomes a NULL pointer. Example to understand what is a NULL pointer. #include <iostream>. using namespace std; int main() {. int *ptr = NULL; // This is a NULL pointer. return 0;
NULL Pointer in C++
The nullptr keyword is a null pointer constant which is a prvalue of type std::nullptr_t. that denotes the pointer literal. C++11 introduced nullptr, the null pointer constant, to remove the ambiguity between 0 and a null pointer. Although the old style NULL macro exists, it is insufficient because it cannot be distinguished from the integer 0 ...
C language Pointer Tutorial...! 👇👇👇https://www.youtube.com/playlist?list=PLqleLpAMfxGC_39XKLj10U_k_n2_V2VEmPlease Subscribe our Channel...!Learn Coding 🙏...
Using Null Pointer Program. NULL is a macro in C, defined in the <stdio.h> header file, and it represent a null pointer constant. Conceptually, when a pointer has that Null value it is not pointing anywhere. If you declare a pointer in C, and don't assign it a value, it will be assigned a garbage value by the C compiler, and that can lead to ...
The NULL constant is defined in many of the header files in the C programming language; including, stdio.h stddef.h, stdlib.h, etc. In C programming, usually, we add stdio.h in the program to use scanf() and printf() functions. So, you don't need to add any extra header files. Later in the code, you can assign any memory location to ptr pointer.
Null Pointer Exception In Java
A quick and thorough guide to 'null': what it is, and how you ...
Null Pointer in C
The NULL pointer is a constant with a value of zero defined in several standard libraries. But the pointer variable can be changed, proof: #include <stdio.h>. int main() {. int *a = NULL; /* Declaration\Definition of pointer and initialization of NULL */. int b; /* Regular Declaration\Definition of variable */.
A pointer can't point to null. It can be a null pointer, which means it doesn't point to anything. And a declared object can't be deleted as long as its name is visible; it only ceases to exist at the end of its scope. &value is a valid address at the time the assignment nullPointer = &value; is executed. It might become invalid later.
What exactly is meant by "de-referencing a NULL pointer"?