Null Pointer

From Embedded Systems Learning Academy
Revision as of 21:05, 18 December 2016 by Proj user18 (talk | contribs) (Example of Null pointer)

Jump to: navigation, search

NULL Pointer is a pointer which is pointing 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. There are, however, certain circumstances where this is not the case. For example, in x86-real mode, the address 0000:0000 is readable and usually writable, hence dereferencing the null pointer is a perfectly valid but typically unwanted action that may lead to undefined but non-crashing behaviour in the application.

NULL

As a matter of style, 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 or ((void *)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. The table under question 5.2 above applies for NULL as well as 0 (an unadorned NULL is equivalent to an unadorned 0). 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 a
   float *f_ptr = (float *)0;
   char *c_ptr = '\0';
   double *d_ptr = (double *)0;

   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;