Dangling, Void , Null ,And Wild Pointers

Dangling, Void, Null, And Wild Pointers

A “dangling pointer” is when a pointer points to a place that’s no longer valid. This can occur in three different situations:

1. De-allocation of memory


// Deallocating a memory pointed by ptr causes
// dangling pointer
 
#include <cstdlib>
#include <iostream>
 
int main()
{
    int* ptr = (int *)malloc(sizeof(int));
 
    // After below free call, ptr becomes a 
    // dangling pointer
    free(ptr); 
     
    // No more  dangling pointer
    ptr = NULL;
}
 

2. Function Call



// The pointer pointing to local variable becomes
// dangling when local variable is not static.
 
#include <iostream>
   
int* fun()
{
    // x is local variable and goes out of
    // scope after an execution of fun() is
    // over.
    int x = 5;
   
    return &x;
}
   
// Driver Code
int main()
{
    int *p = fun();
    fflush(stdin);
   
    // p points to something which is not
    // valid anymore
    std::cout << *p;
   
    return 0;
}

Output

0

Whereas, if x is a static variable, then the problem that occurs in the above program won’t happen.

// The pointer pointing to local variable doesn't
// become dangling when local variable is static.
 
#include <iostream>
using namespace std;
 
int *fun()
{
    // x now has scope throughout the program
    static int x = 5;
 
    return &x;
}
 
int main()
{
    int *p = fun();
    fflush(stdin);
     
    // Not a dangling pointer as it points
    // to static variable.
    cout << *p;
     
    return 0;
}

Output

5

3. Variable goes out of scope 

void main()
{
   int *ptr;
   .....
   .....
   {
       int ch;
       ptr = &ch;
   } 
   .....   
   // Here ptr is dangling pointer
}

Void Pointer

A “void pointer” (void *) is a special kind of pointer that doesn’t have a specific data type. It can point to data of any type. If you assign it the address of a char, it becomes a char pointer; if you assign it an int address, it’s an int pointer, and so on. You can convert any pointer type to a void pointer, allowing it to point to any value.

Some key points to remember:

  • You can’t directly access the data pointed to by a void pointer; you need to typecast it to the appropriate data type first.
  • You can’t perform pointer arithmetic on void pointers because they lack a specific size and concrete data type.

Example



#include <stdlib.h>
 
int main()
{
    int x = 4;
    float y = 5.5;
 
    // A void pointer
    void* ptr;
    ptr = &x;
 
    // (int*)ptr - does type casting of void
    // *((int*)ptr) dereferences the typecasted
    // void pointer variable.
    printf("Integer variable is = %d", *((int*)ptr));
 
    // void pointer is now float
    ptr = &y;
    printf("\nFloat variable is = %f", *((float*)ptr));
 
    return 0;
}

Output

Integer variable is = 4
Float variable is = 5.500000

Null Pointer

A “NULL Pointer” is like a pointer that points to nothing. When you don’t have a specific address to assign to a pointer, you can use NULL to indicate that it’s not pointing to anything.



#include <iostream>
using namespace std;
 
int main()
{
    // Null Pointer
    int *ptr = NULL;
     
    cout << "The value of ptr is " << ptr;
    return 0;
}

Output

The value of ptr is 0

Important Points:

  1. NULL Pointer vs. Uninitialized Pointer:
  • An uninitialized pointer contains an undefined value, which means it’s pointing to a random or unpredictable memory location.
  • A NULL pointer contains a defined value that is intentionally set to indicate it’s not pointing to a valid memory location.

2. NULL Pointer vs. Void Pointer:

  • A NULL pointer is a specific value, indicating that a pointer doesn’t point to a valid address.
  • A void pointer is a data type that doesn’t specify any particular data type, but it’s used to point to data of any type.

Wild Pointer

A “wild pointer” is a pointer that hasn’t been set to any specific value, not even NULL. It might start with a random or garbage value that doesn’t necessarily point to a valid memory address.


int main()
{
    int *p;  /* wild pointer */
 
    int x = 10;
 
    // p is not a wild pointer now
    p = &x;
 
    return 0;
}

FAQ- Dangling, Void , Null and Wild Pointers

Q1. What is meant by dangling pointer and null pointer?

Ans.
A “null pointer” doesn’t point to any memory location; it’s intentionally set to indicate “nothing.”
A “dangling pointer” points to a memory location that used to be valid but has been deallocated or freed, making it potentially unsafe to use.

Q2. What is the use of dangling pointer in C++?

Ans. “Dangling pointers” occur when you have pointers in your program that are still there in the stack, but they’re pointing to memory that is no longer valid or has been deallocated.

Q3. What is void pointer in C?

Ans. In C, a “void pointer” is a special type of pointer that doesn’t have a specific data type associated with it. It can point to a location in memory where data is stored, but the data type is not specified.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment