Interview Questions
Articles

Explain dangling pointer, null pointer, void pointer, wild pointer.

States of Pointers - Null Pointer, Void Pointer, Dangling Pointer, Wild Pointer

A Pointer can be a Null Pointer or a Void Pointer or a Dangling Pointer or a Wild Pointer depending upon its state, which is defined by the operations performed on it or value it holds.

Null pointer:

Pointer assigned value NULL.

Example of null pointer:

  1. int *ptr=NULL;
  2. int *ptr = new int;
    delete ptr;
    ptr = NULL;
    // ptr is null pointer


Void pointer:

Pointer of type void which can take address assigned from any other pointer (except function pointer) and can be reassigned to other pointer type. Void pointers cannot be dereferenced. They act as place holder.

Example of void pointer:

void *ptr;


Dangling pointer:

Pointer assigned some memory and then free that memory. After freeing when not assigned to NULL it still points to same memory address, such pointer are dangling pointer.

Example of dangling pointer:

int* ptr=new int;
<some ops with ptr >
delete ptr; // at this stage, ptr is dangling pointer


Wild pointer:

Pointer which are not initialized during its definition holding some junk value( a valid address) are Wild pointer.

Example of wild pointer:

int *ptr;