NULL Pointer in C: What It Is, Why It Matters and How to Check
A NULL pointer points to nothing (address 0). Learn why every pointer should be initialized to NULL, how to check it and what a NULL dereference crash is.
What is a NULL pointer?
NULL (defined as address 0 in <stdio.h>/<stddef.h>). It is the standard way to say: "this pointer is currently not pointing to any valid memory."int *p = NULL; /* safe: clearly points nowhere */
Why initialize pointers to NULL?
An uninitialized pointer contains a random garbage address (a wild pointer). Writing through it corrupts unknown memory. A NULL pointer is safe because:
- You can test it:
if (p == NULL)— with garbage you cannot test anything. - Dereferencing NULL crashes immediately and loudly (segmentation fault), so the bug is found fast, instead of silently corrupting data.
How to check a NULL pointer
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int*)malloc(sizeof(int) * 1000000000); /* may fail */
if (p == NULL) { /* ALWAYS check malloc result */
printf("Memory allocation failed!\n");
return 1;
}
*p = 10;
printf("Value: %d\n", *p);
free(p);
p = NULL; /* good habit after free */
return 0;
}
Three standard checking styles — all equivalent:
if (p == NULL) { ... }
if (!p) { ... }
if (p != NULL) { ... } /* or simply: if (p) */
NULL pointer dereference: the crash
int *p = NULL;
*p = 5; /* CRASH: segmentation fault */
printf("%d", *p); /* CRASH: reading NULL also crashes */
The free + NULL pattern
free(p);
p = NULL; /* prevents dangling pointer + double free */
Setting a pointer to NULL right after free() gives two protections: dereferencing it later crashes clearly instead of using freed memory, and calling free(NULL) a second time is guaranteed safe by the C standard.
NULL pointer क्या है?
NULL होती है (<stdio.h>/<stddef.h> में address 0 defined). यह कहने का standard तरीका है: "यह pointer अभी किसी valid memory को point नहीं कर रहा."int *p = NULL; /* safe: साफ पता है कि कहीं point नहीं करता */
Pointers को NULL से initialize क्यों करें?
Uninitialized pointer में random garbage address होता है (wild pointer). उससे write करना unknown memory corrupt करता है. NULL pointer safe है क्योंकि:
- आप उसे test कर सकते हैं:
if (p == NULL)— garbage को test नहीं कर सकते. - NULL dereference तुरंत और साफ crash करता है (segmentation fault), जिससे bug जल्दी मिल जाता है — data चुपचाप corrupt नहीं होता.
NULL pointer कैसे check करें
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int*)malloc(sizeof(int) * 1000000000); /* fail हो सकता है */
if (p == NULL) { /* malloc result हमेशा check करें */
printf("Memory allocation failed!\n");
return 1;
}
*p = 10;
printf("Value: %d\n", *p);
free(p);
p = NULL; /* free के बाद अच्छी habit */
return 0;
}
तीन standard checking styles — सब equivalent:
if (p == NULL) { ... }
if (!p) { ... }
if (p != NULL) { ... } /* या सिर्फ: if (p) */
NULL pointer dereference: crash
int *p = NULL;
*p = 5; /* CRASH: segmentation fault */
printf("%d", *p); /* CRASH: NULL पढ़ना भी crash करता है */
free + NULL pattern
free(p);
p = NULL; /* dangling pointer + double free से बचाता है */
free() के तुरंत बाद pointer को NULL करने से दो protections मिलती हैं: बाद में dereference करने पर freed memory use होने की बजाय साफ crash होता है, और दूसरी बार free(NULL) call करना C standard के हिसाब से guaranteed safe है.
Frequently Asked Questions
What is a NULL pointer in C?
A NULL pointer is a pointer that holds the value NULL (address 0), meaning it intentionally points to no valid memory location.
What happens if we dereference a NULL pointer?
Dereferencing NULL causes undefined behaviour; on most systems the program crashes immediately with a segmentation fault.
Is free(NULL) safe in C?
Yes. The C standard guarantees free(NULL) does nothing, which is why setting a pointer to NULL after free protects against double-free bugs.