7

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?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Namandeep_Kaur
  • 368
  • 1
  • 3
  • 11

6 Answers6

7

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.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • 1
    So, you don't know DOS anymore? OR any embedded system? Because those are unlikely to immediately crash on misuse of nullpointers. – Deduplicator Aug 25 '15 at 18:42
  • 3
    My DOS experience is extremely limited. And I do not have any experience with embedded systems. – SergeyA Aug 25 '15 at 18:44
4

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.

ryyker
  • 22,849
  • 3
  • 43
  • 87
Joe
  • 380
  • 2
  • 6
  • 6
    `NULL` is just a concept. It is represented by `0` in C, but that doesn't necessarily mean that it points to address 0x0. http://stackoverflow.com/questions/7613175/in-c-is-there-a-difference-between-a-null-pointer-and-a-pointer-that-points-to – PC Luddite Aug 25 '15 at 18:48
  • @PCLuddite - I meant due to NULL normally being defined as 0 , the pointer will normally thus point to 0x0 – Joe Aug 25 '15 at 18:49
  • 2
    But 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. – PC Luddite Aug 25 '15 at 18:52
  • @NDeepK - what do you mean? – Joe Aug 25 '15 at 18:54
  • 3
    The language only guarantees that the nullpointer, whatever address it might be represented by, is distinct from any valid pointer. – Deduplicator Aug 25 '15 at 18:55
  • @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. – Joe Aug 25 '15 at 18:56
  • 1
    @Joe I'm sure that `NULL` in MSVC is defined as `0` or `(void*)0`, but it still doesn't mean that when it's compiled that it actually points to `0x0`. – PC Luddite Aug 25 '15 at 18:58
  • @PCLuddite - with my debugger (using MSVC), a null pointer's address is 0x0 – Joe Aug 25 '15 at 19:01
  • Alright! 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. – Namandeep_Kaur Aug 25 '15 at 19:10
  • 4
    @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`). – PC Luddite Aug 25 '15 at 19:15
  • `int i = 0; printf("%zu %zu", sizeof i, sizeof NULL); /* => 4 8 */` – Student Aug 11 '21 at 16:43
3

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

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • @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)? – Namandeep_Kaur Aug 25 '15 at 18:58
  • For the most part, yes. Unless you have specifically assigned it NULL. – MSU_Bulldog Aug 25 '15 at 19:01
  • @NDeepK It means that the null pointer is guaranteed to be different than any other pointer. The environment defines what it is. – PC Luddite Aug 25 '15 at 19:10
  • @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. – MSU_Bulldog Aug 25 '15 at 19:13
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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

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);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

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.

haccks
  • 104,019
  • 25
  • 176
  • 264