Reputation: 378
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?
Upvotes: 8
Views: 20345
Reputation: 380
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
.
Upvotes: 4
Reputation: 153498
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);
Upvotes: 3
Reputation: 134336
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.
Upvotes: 3
Reputation: 106012
Yes. There is a difference in uninitialized and a null pointer. An uninitialized pointer can point to any (unknown) memory location . A null pointer initialized with NULL
; implementation-defined null pointer constant.
Upvotes: 2
Reputation: 3519
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
Upvotes: 3
Reputation: 62583
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.
Upvotes: 7