Dangling pointer
A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer. There are three different ways where Pointer acts as dangling pointer
1. De-allocation of memory
C++
#include <cstdlib>
#include <iostream>
int main()
{
int * ptr = ( int *) malloc ( sizeof ( int ));
free (ptr);
ptr = NULL;
}
|
C
#include <stdlib.h>
#include <stdio.h>
int main()
{
int *ptr = ( int *) malloc ( sizeof ( int ));
free (ptr);
ptr = NULL;
}
|
2. Function Call
C++
#include <iostream>
int * fun()
{
int x = 5;
return &x;
}
int main()
{
int *p = fun();
fflush (stdin);
std::cout << *p;
return 0;
}
|
C
#include<stdio.h>
int *fun()
{
int x = 5;
return &x;
}
int main()
{
int *p = fun();
fflush (stdin);
printf ( "%d" , *p);
return 0;
}
|
The above problem doesn’t appear (or p doesn’t become dangling) if x is a static variable.
C++
#include <iostream>
using namespace std;
int *fun()
{
static int x = 5;
return &x;
}
int main()
{
int *p = fun();
fflush (stdin);
cout << *p;
return 0;
}
|
C
#include <stdio.h>
int *fun()
{
static int x = 5;
return &x;
}
int main()
{
int *p = fun();
fflush (stdin);
printf ( "%d" ,*p);
}
|
3. Variable goes out of scope
void main()
{
int *ptr;
.....
.....
{
int ch;
ptr = &ch;
}
.....
// Here ptr is 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 is can be any. If we assign address of char data type to void pointer it will become 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.
Important Points
- void pointers cannot be dereferenced. It can however be done using typecasting the void pointer
- Pointer arithmetic is not possible on pointers of void due to lack of concrete value and thus size.
Example:
C
#include <stdlib.h>
int main()
{
int x = 4;
float y = 5.5;
void * ptr;
ptr = &x;
printf ( "Integer variable is = %d" , *(( int *)ptr));
ptr = &y;
printf ( "\nFloat variable is = %f" , *(( float *)ptr));
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int x = 4;
float y = 5.5;
void * ptr;
ptr = &x;
cout << "Integer variable is = " << *(( int *)ptr)
<< endl;
ptr = &y;
cout << "Float variable is = " << *(( float *)ptr) << endl;
return 0;
}
|
Output
Integer variable is = 4
Float variable is = 5.500000
Refer void pointer article for details.
NULL Pointer
NULL Pointer is a pointer which is pointing to nothing. In case, if we don’t have address to be assigned to a pointer, then we can simply use NULL.
C++
#include <iostream>
using namespace std;
int main()
{
int *ptr = NULL;
cout << "The value of ptr is " << ptr;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int *ptr = NULL;
printf ( "The value of ptr is %p" , ptr);
return 0;
}
|
Output
The value of ptr is 0
Important Points
- NULL vs Uninitialized pointer – An uninitialized pointer stores an undefined value. A null pointer stores a defined value, but one that is defined by the environment to not be a valid address for any member or object.
- NULL vs Void Pointer – Null pointer is a value, while void pointer is a type
Wild pointer
A pointer that has not been initialized to anything (not even NULL) is known as wild pointer. The pointer may be initialized to a non-NULL garbage value that may not be a valid address.
CPP
int main()
{
int *p;
int x = 10;
p = &x;
return 0;
}
|
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!