Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Dangling Pointer
2.1.
De-allocation of memory
2.2.
Function Call
2.3.
Variable goes out of scope
3.
Void pointer
3.1.
Advantages
4.
Null pointer
5.
Wild Pointer
6.
Frequently Asked Questions
6.1.
What are pointers?
6.2.
What is the difference between a null pointer and a void pointer?
6.3.
What is the disadvantage of using void pointers?
7.
Conclusion
Last Updated: Mar 27, 2024

Dangling, Void, Null and Wild Pointers

Introduction

C++ is a very powerful programming language that allows the programmer to allocate memory dynamically with the help of pointers. Every variable has a memory location, and with that memory location, an address is associated that can be accessed using the ‘&’ ampersand operator. A pointer is a type of variable whose value is the address of another variable. A pointer can be of any valid C++ type. It is declared using the ‘*’. The actual data type of the value of all pointers is the same,: a long hexadecimal number used to represent a memory address. There are different types of pointers in C++, such as dangling, void, null and wild pointers. In this blog, we will discuss about them in detail.

Also see, Literals in C.Fibonacci Series in C++

Dangling Pointer

It is a type of pointer which points to a memory location that has already been deleted or freed. There are three ways in which pointers behave as a dangling pointer which are as follow:

De-allocation of memory

Code

#include <stdlib.h>
#include <stdio.h>
int main()
{
    // declaring a pointer of char type
    char *dangling_ptr = (char *)malloc(sizeof(char));
  
    // ptr becomes a dangling pointer
    free(dangling_ptr); 
}

Function Call

Code

#include<stdio.h>
int *helper()
{
    // we have declared a variable x of integer type
    // x goes out of scope when helper() is executed
    int x = 100;
    return &x;
}
int main()
{
    // declaring a pointer
    int *dangling_ptr = helper();
    fflush(stdin);
  
    // dangling_ptr is pointing to something which is not valid anymore
    printf("%d", *dangling_ptr);
    return 0;
}


Output

Garbage Address

Variable goes out of scope

Code

#include<stdio.h>
int main()
{
    int *ptr;
  {
      int x;
      ptr = &x;
  } 
  // Here ptr becomes a dangling pointer
    printf("%d",*ptr);
}


Output

Garbage Value

 

Try and compile with online c++ compiler.

Void pointer

It is the type of pointer which points to some data location in storage/memory which doesn't have any specific type. Such a pointer can point to any type of data, that is, any pointer type is convertible to a void pointer. Void pointers are also known as general-purpose pointers. There are a few limitations to void pointers:

  • Void pointers can not be used for pointer arithmetic because they are of a concrete size.
  • It cannot be dereferenced. However, it can be done using typecasting the void pointer.


Advantages

  • Functions such as malloc() and calloc() return void* type, which allows such functions to allocate memory of any data type
  • These pointers are also used to implement generic functions in the C programming language.
     

Code 

#include<stdlib.h>
#include <cstdio>
int main()
{
    int var1 = 100;
    float var2 = 120.5;
      
    //declaring and intializing a void pointer
    void *ptr;
    ptr = &var1;
  
    // perform Type-casting of void by (int*)ptr
    // void pointer variable
    printf("Integer value of variable using the void pointer is = %d", *( (int*) ptr) );
  
    // void pointer is now float
    ptr = &var2; 
    printf("\nFloat value of variable using the void pointer is = %f", *( (float*) ptr) );
  
    return 0;
}


Output

Integer value of variable using the void pointer is = 100
Float value of variable using the void pointer is = 120.500000


Explanation

In the above example, we have first created a void type pointer named “ptr”, and it is assigned the address of variable “var1” which is of integer type. We have used the void type pointer to print the variable “var1” value by performing typecasting. Then, the void pointer “ptr” is assigned the address of variable “var2” which is of float type. And then, using the typecasting, we have used the void pointer to print the variable “var2” value.

Null pointer

It is the type of pointer which points to nothing, it is useful in cases when we don't have an address which is to be assigned to a pointer. Some important points regarding null pointers are:

  • A null pointer is a value, whereas a void pointer is a type
  • A null pointer stores a defined value, whereas uninformed pointer stores an undefined value.
  • A null pointer can be used to initialize a pointer variable when the memory address which is to be assigned is unknown.
  • It can also be used to pass a null pointer to a function argument if we wish not to pass any valid memory address.
     

Code

#include <stdio.h>
int main()
{
    // declaring a null-pointer
    int *nul_ptr = NULL;
      
    printf("The value of ptr is : %p", nul_ptr);
    return 0;
}


Output

The value of ptr is : (nil)

Wild Pointer

It is a type of pointer that has not been initialized to anything, not even a NULL value. 

Code

#include <stdio.h>
int main()
{
    // declaring a wild pointer
    int *ptr; 
    
    int val = 47;
    // ptr is not a wild pointer anymore
    ptr = &val;
    
    return 0;
}

Frequently Asked Questions

What are pointers?

A pointer is a type of variable whose value is the address of another variable.
 

What is the difference between a null pointer and a void pointer?

The difference between a null and a void pointer is that a null pointer is a value, whereas the void pointer is a type.
 

What is the disadvantage of using void pointers?

The main disadvantage of using void pointers is that they cannot be used for pointer arithmetic.

Conclusion

In this article, we have extensively discussed the different types of pointers, such as Dangling, Void, Null and Wild pointers.

After reading about dangling, void, null and wild pointers, are you not feeling excited to read/explore more articles on pointers? Don't worry; Coding Ninjas has you covered. To learn about pointers and references, what is the size of a pointer in C, and how to clone a linked list with a random pointer

Recommended Reading: AMD vs Intel

If you wish to enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, and many more, then you should check out our Guided path column at Coding Ninjas Studio. We at Coding Ninjas Studio organize many contests in which you can participate. You can also prepare for the contests and test your coding skills by giving the mock test series available. In case you have just started the learning process, and your dream is to crack major tech giants like Amazon, Microsoft, etc., then you should check out the most frequently asked problems and the interview experiences of your seniors that will surely help you in landing a job in your dream company. 

Do upvote if you find the blogs helpful.

Happy Learning!

Live masterclass