Null Pointer | Void Pointer | Dangling pointer – C | C++ Notes

Notes on NULL pointer, void pointer and dangling pointer in C and C++ with examples, uses and answer to questions e.g. why to use null pointer, what is void pointer, what is dangling pointer and how to avoid it and why we should avoid it , difference between NULL pointer and void pointer etc.

NULL POINTER in C, C++

We can initialize a pointer  with pointer value 0 or NULL. Its meaning is the pointer is pointing to nothing. It is called a null pointer.

NULL value is defined as either 0 in C++ and (void *)0 in C. Below is the snap shot of the code from stdlib.h header file that is used in C or C++ program..

#ifdef __cplusplus //C++
#define NULL    0
#else
#define NULL    ((void *)0)//C
#endif

Example: int * p = NULL; // this is integer pointer that points to nothing if the pointer value is null.

Note that null pointer dereference – (*p – dereferencing a null pointer) causes an STATUS_ACCESS_VIOLATION exception.

Example:

In below example, we have initialized the pointer with NULL and during accessing the int value from the location we are checking if memory is allocated properly. If malloc fails it returns NULL. If we try to access value with invalid memory the program will crash or run time error for heap corruption.

#include <stdlib.h>
#include <stdio.h>
int main()
{
	int *iPtr = NULL;	//NULL pointer assignment
	int a=10;

	//allocate memory for integer
    iPtr = (int *)malloc(sizeof(int));
	//assign value
	iPtr=&a;

	//Check if memory is allocated
	if (iPtr != NULL){
		//print value
		printf("val = %d",*iPtr);
	}
 
   //De allocate memory
    free(iPtr); 
	

}

Why to use NULL pointer?

Use as a marker – For example, to indicate a pointer that has not been allocated memory or to check end of a linked list or to check if a tree has any child note or leaf etc.

 Another example, If malloc can’t allocate memory, it returns a null pointer. So, we can check the pointer by comparing with NULL, if a valid memory is allocated. Also, check to free a valid memory etc.

Dangling Pointer in C, C++

Dangling pointer in C and C++  is a pointer pointing to memory location which is deleted or freed , but still has the invalid address. Generally, the dangling pointers occurs in a program with following steps

  • Allocate the memory dynamically using malloc, calloc or new (C++) and store the address in a pointer.
  • When done with the memory free or delete the memory location.
  • Don’t initialize the pointer with some defined value e.g. NULL then it will contain some address which is invalid location now.

Now, this pointer is called dangling pointer. If we process this pointer further in the program then the program will crash as it will not find valid memory. So, It is always a good practice to initialized the pointer with NULL to avoid it to be dangling pointer.
Dangling pointer example:

After freeing the allocated memory, again it will be assigned to NULL to avoid dangling. After freeing memory the iPter is dangling pointer as store the address but that is invalid now.Hence, assign it to NULL to avoid dangling pointer.

int main()
{
	int *iPtr = NULL;	
	int a=10;	
    iPtr = (int *)malloc(sizeof(int));	
	iPtr=&a;
	//Check if memory is allocated
	if (iPtr != NULL){		
		printf("val = %d",*iPtr);
	}
 
   //De allocate memory
    free(iPtr); 

	//After freeing memory the iPter is 
	//dangling pointer as store the address but
	//that is invalid now.
	

	//Hence, assing it to NULL to avoid dangling
	iPtr = NULL;
	

}

VOID POINTER in C, C++

Void pointer in C and C++ is a generic pointer that can point to any data types e.g. int, float and char etc. in other words,  void pointer – void * – is a pointer that points to some data location in storage, which doesn’t have any specific type. Void pointer is also known as generic pointer in c and c++ programs.

Note that we can store any data type in void pointer, but, when we want to retrieve data from void pointer then we need to type cast to the specific data type.

Void Pointer Example

In below program, we have created a void pointer and assign the address of int variable. Value is retrieved back by typecasting void pointer to int pointer and De-referencing it.

int main()
{
	int a =5;
	int result;
	//void pointer
	void *vPtr;
	//Assign address of int a
	//now void pinter is pointing to int data type
	vPtr= &a;	

	//Retrive back the value of a from void
	//pointer.
	//First we need to type cast void pointer to 
	//integer pointer ((int*) vPtr)then access the value 
	//by dereferencing it 

	result =  *( (int*) vPtr) ;
	printf("Value of a: %d",result);			

}

Output: Value of a: 5

Another example of void pointer
Example: In below C program, malloc returns a void pointer on memory allocation. Since, we have allocated for integer data type, so, we need to type cast to int pointer type. see the syntax.

int main()
{
	//malloc returns void pointer
    int *iPtr = (int *)malloc(sizeof(int));		

}

Difference between NULL pointer and void pointer

NULL pointer and void pointer are totally different concepts and no need to compare them. NULL pointer is a value, whereas void pointer is a type

Null pointer is a special reserved value of a pointer and any type of pointer can have reserved value. Void pointer is a specific pointer type say generic type- void * – a pointer that points to some data location in storage, which doesn’t have any specific type.

Related interview Question :

What is size of void pointer?

Related Posts