Difference between revisions of "Null Pointer"

From Embedded Systems Learning Academy
Jump to: navigation, search
(C++)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
NULL Pointer is a pointer which points to nothing. A null pointer in most programming languages means "no value". Null pointers represent conditions such as the end of a list of unknown length or the failure to perform some operation.
+
A Null Pointer is a pointer which points to nothing. A null pointer in most programming languages means "no value". Null pointers represent conditions such as the end of a list of unknown length or the failure to perform some operation.
  
 
A null pointer is different from an uninitialized pointer: A null pointer will always compare unequal to any pointer that points to a valid object, while an uninitialized pointer might point anywhere. Uninitialized pointer may be equal to other, valid pointers; or it might compare equal to null pointers.
 
A null pointer is different from an uninitialized pointer: A null pointer will always compare unequal to any pointer that points to a valid object, while an uninitialized pointer might point anywhere. Uninitialized pointer may be equal to other, valid pointers; or it might compare equal to null pointers.
Line 65: Line 65:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<BR/>
 
<BR/>
 
+
Nullptr is a null pointer constant of type std::nullptr_t. Although the keyword nullptr can be used without including any headers, if the code uses the type std::nullptr_t, then you must define it by including the header <cstddef>.
  
 
== Check for a Null pointer ==
 
== Check for a Null pointer ==
Line 85: Line 85:
 
         printf("ptr is a null pointer.\n");
 
         printf("ptr is a null pointer.\n");
 
   }
 
   }
    if(f_ptr)
+
  if(f_ptr)
 
   {
 
   {
 
       printf("f_ptr is not a null pointer.\n");
 
       printf("f_ptr is not a null pointer.\n");

Latest revision as of 21:35, 18 December 2016

A Null Pointer is a pointer which points to nothing. A null pointer in most programming languages means "no value". Null pointers represent conditions such as the end of a list of unknown length or the failure to perform some operation.

A null pointer is different from an uninitialized pointer: A null pointer will always compare unequal to any pointer that points to a valid object, while an uninitialized pointer might point anywhere. Uninitialized pointer may be equal to other, valid pointers; or it might compare equal to null pointers.

C

Two null pointers of any type are guaranteed to compare equal. The macro NULL is defined as an implementation-defined null pointer constant,which in C99 can be expressed as the integer value 0 converted implicitly or explicitly to the type void*.

Dereferencing the NULL pointer typically results in an attempted read or write from memory that is not mapped - triggering a segmentation fault. This shows up as program crash or be transformed into an exception that can be caught.

Generally, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.

NULL

As a good practice, many programmers prefer not to have 0's scattered through their programs to represent pointers. Therefore, the preprocessor macro NULL is defined (by headers such as <stdio.h> and <stddef.h>) as a null pointer constant, typically 0. A programmer who wishes to make explicit the distinction between 0 the integer and 0 the null pointer constant can then use NULL whenever a null pointer is required.

#define NULL 0


Using NULL is a stylistic convention only; the preprocessor turns NULL back into 0 which is then recognized by the compiler, in pointer contexts, as before. In particular, a cast may still be necessary before NULL (as before 0) in a function call argument. NULL is macro constant defined in following header files –

  1. stdio.h
  2. alloc.h
  3. mem.h
  4. stddef.h
  5. stdlib.h

NULL should be used only as a pointer constant. It is bad practice to use NULL to assign 0 value to a variable.

Example of Null pointer

#include <stdio.h>
int main()
{
   int  *ptr = NULL;           //Best way to assign the pointer Null to a pointer variable. Works for any data type.
   float *f_ptr = (float *)0;  //Another way to assign the pointer Null to a pointer variable of type float.
   char *c_ptr = '\0';         //Assigning the pointer Null to a pointer variable of type char.
   double *d_ptr = (double *)0;//Another way to assign the pointer Null to a pointer variable of type double.

   printf("The value of ptr is %u\n",ptr);
   printf("The value of f_ptr is %u\n",f_ptr);
   printf("The value of c_ptr is %u\n",c_ptr);
   printf("The value of d_ptr is %u\n",d_ptr);
   return 0;
}


Output

The value of ptr is 0
The value of f_ptr is 0
The value of c_ptr is 0
The value of d_ptr is 0


C++

In C++ the integer literal for zero has been traditionally preferred to represent a null pointer constant. However, C++11 has introduced an explicit nullptr constant to be used instead. Example

int* x = nullptr;
myclass* obj = nullptr;


Nullptr is a null pointer constant of type std::nullptr_t. Although the keyword nullptr can be used without including any headers, if the code uses the type std::nullptr_t, then you must define it by including the header <cstddef>.

Check for a Null pointer

#include <stdio.h>
int main()
{
   int  *ptr = NULL;
   float *f_ptr = NULL;
   float y=10.0;
   f_ptr=&y;
   
   if(ptr)
   {
       printf("ptr is not a null pointer.\n");
   }
   else
   {
        printf("ptr is a null pointer.\n");
   }
   if(f_ptr)
   {
       printf("f_ptr is not a null pointer.\n");
   }
   else
   {
        printf("f_ptr is a null pointer.\n");
   }
   

   return 0;
}


Output

ptr is a null pointer.
f_ptr is not a null pointer.


If all unused pointers are assigned a null value, accidental misuse of an uninitialized pointer can be avoided. Many times, uninitialized variables hold some junk values and it becomes difficult to debug the program.