Open In App

Difference between Dangling pointer and Void pointer

Last Updated : 13 Jul, 2021
Like Article

Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer:

Void pointer: Void pointer is a specific pointer type – void * – a pointer that points to some data location in storage, which doesn’t have any specific type. Void refers to the type. Basically, the type of data that it points to can be any. If we assign the address char data type to a void pointer, it will become a char Pointer, if int data type, then int pointer, and so on. Any pointer type is convertible to a void pointer. Hence, it can point to any value. Below are some important points regarding void pointers:

  • void pointers cannot be dereferenced. However, it can be done using typecasting the void pointer
  • Pointer arithmetic is not possible on pointers of void due to lack of concrete value and size.

Tabular Difference Between Dangling Pointer and Void Pointer:

Dangling Pointer

Void Pointer

A dangling pointer is a pointer that occurs at the time when the object is de-allocated from memory without modifying the value of the pointer. A void pointer is a pointer that can point to any data type.
It points to the deleted object. A void pointer can be assigned the address of any data type.
It usually occurs at the object destruction time. The representation of a pointer to the void is the same as the pointer of the character type.
Dangling pointer errors can only be avoided just by initializing the pointer to one NULL value. A void pointer can store an object of any type.
The dangling pointer will be with a free() function in C. It is also called a general-purpose pointer.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads