Difference between uninitialized and null pointer
Solution 1
Take an uninitialized pointer:
int* ptr;//points to any location in memory
Take a null pointer:
int* ptr = NULL;//normally points to 0x0 (0)
Both would cause undefined behaviour if dereferenced. NULL
is often defined as 0
.
Solution 2
Well, the difference is exactly that. Null pointer is initialized to null, and as such has defined meaning. You can check it for null, and dereferencing it will (on all platforms I know) cause a program to crash with meaningful diagnostics. You can also use null pointers in some specific hacks. Unitinialized pointers, on the other hand, are just random and should be avoided.
Solution 3
The base difference is that an uninitiated pointer has an indeterminate value whereas a NULL pointer has a defined value which is NULL
.
Regarding the NULL pointer, from C11
, chapter §6.3.2.3
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.
FWIW, the macro NULL
is defined in <stddef.h>
, as a null pointer constant.
Solution 4
After accept answer
void foo(void) {
void *uninitialized_pointer;
void *null_pointer = null_pointer_generator();
...
}
uninitialized_pointer
in uninitialized. It may have a valid pointer value in it. It may have a value that compares to NULL
. It may have not have any pointer value in it. C does not have a defined method to even copy or print its value.
// These are undefined behavior.
void *another_pointer = uninitialized_pointer;
unsigned x = uninitialized_pointer*0;
printf("%p\n", uninitialized_pointer);
Code can assign uninitialized_pointer
, compute its size or pass its address.
// These are defined behavior.
uninitialized_pointer = malloc(1);
uninitialized_pointer = NULL;
printf("%zu\n", sizeof uninitialized_pointer);
foo(&uninitialized_pointer);
The variable null_pointer
has a value that compares equally to the null pointer constant (see below) and so in a null pointer. A null pointer may be unique bit pattern or there may be many of them in a system. They all compare equally to the null pointer constant and to each other. A null pointer may or may not be a valid address in the system although it will not compare equally to any object, variable, member, function in your program.
Attempting to dereference a null pointer is undefined behavior: it may cause a seg-fault - it may not.
NULL
is the null pointer constant. When assigned to a pointer, that pointer is a null pointer. When 0
is assigned to a pointer, that pointer is a null pointer. These may/may not be different null pointers. They will compare equally to each other.
void *null_pointer1 = NULL;
void *null_pointer2 = 0;
// The 2 pointer may/may not have the same bit pattern.
printf("%p\n%p\n", null_pointer1, null_pointer2);
// They will always compare as equal.
printf("%d\n", null_pointer1 == null_pointer2);
// Always compare as unequal.
int x;
printf("%d\n", null_pointer1 == &x);
Solution 5
An uninitialized pointer stores an undefined value.
A null pointer stores a defined value, but one that is defined by the environment to not be a valid address for any member or object.
Ok... I googled it for you, heres the link: Null pointer vs uninitialized pointer
Comments
-
Namandeep_Kaur almost 2 years
Is there any difference between null pointer and uninitialized pointer? This question was asked in one of the interviews. Could you please explain the difference that they have?
-
Deduplicator over 8 yearsSo, you don't know DOS anymore? OR any embedded system? Because those are unlikely to immediately crash on misuse of nullpointers.
-
SergeyA over 8 yearsMy DOS experience is extremely limited. And I do not have any experience with embedded systems.
-
PC Luddite over 8 years
NULL
is just a concept. It is represented by0
in C, but that doesn't necessarily mean that it points to address 0x0. stackoverflow.com/questions/7613175/… -
Joe over 8 years@PCLuddite - I meant due to NULL normally being defined as 0 , the pointer will normally thus point to 0x0
-
PC Luddite over 8 yearsBut that's not true. NULL is defined in the language as 0, but upon compilation, it may point to somewhere else that the hardware defines as NULL.
-
Joe over 8 years@NDeepK - what do you mean?
-
Deduplicator over 8 yearsThe language only guarantees that the nullpointer, whatever address it might be represented by, is distinct from any valid pointer.
-
Joe over 8 years@PCLuddite the only reason for me writing that
int* ptr = NULL
makes a pointer point to 0x0 is that MSVC does that so I guess I should have researched further. -
Namandeep_Kaur over 8 years@MSU_Bulldog Sir, even I googled it, but could not understand the meaning of "defined by the environment", as mentioned in the definition of null pointer. Does it mean that a variable can't have an address that is 0 (null)?
-
PC Luddite over 8 years@Joe I'm sure that
NULL
in MSVC is defined as0
or(void*)0
, but it still doesn't mean that when it's compiled that it actually points to0x0
. -
Joe over 8 years@PCLuddite - with my debugger (using MSVC), a null pointer's address is 0x0
-
MSU_Bulldog over 8 yearsFor the most part, yes. Unless you have specifically assigned it NULL.
-
PC Luddite over 8 years@NDeepK It means that the null pointer is guaranteed to be different than any other pointer. The environment defines what it is.
-
Namandeep_Kaur over 8 yearsAlright! So it means that an uninitialized pointer shows an undefined behavior as one would not know which address will it point to. But, a pointer initialized to null (i.e., a null pointer) will point to 0x0 so that it doesn't show an undefined behavior which could sometimes lead to crash.
-
MSU_Bulldog over 8 years@NDeepK is right. It could either be assigned 0 or something else, just depends on what the environment defines the pointer as. Therefore do not ever assume it will be 0 or NULL.
-
PC Luddite over 8 years@NDeepK Not exactly. Using an uninitialized pointer is undefined behavior, but dereferencing a NULL pointer is also undefined behavior (both could lead to a crash). The difference is that an uninitialized pointer could be anything, but a NULL pointer has a definite value (i.e.
NULL
). -
Student over 2 years
int i = 0; printf("%zu %zu", sizeof i, sizeof NULL); /* => 4 8 */