
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Dangling, Void, Null and Wild Pointers in C++
In C++, direct memory access is possible using pointers. However, the improper use of pointers can lead to problems such as dangling pointers, null pointers, void pointers, and wild pointers. You must have to fix these problems properly for correct code compilation and execution. Let us learn how these problems occur and how you can fix them.
Dangling Pointer
A dangling pointer is a variable that points to invalid or freed memory, causing errors if accessed. It is like calling a disconnected phone number. When the local variable is not static, the pointer pointing to it becomes dangling.
Syntax
Following is the syntax to the dangling pointer:
int *show(void) { int n = 76; /* ... */ return &n; }
Example
This program causes undefined behavior when dereferencing the pointer.
#include <iostream> using namespace std; int* show() { int n = 76; return &n; } int main() { int *ptr = show(); cout << "Value: " << *ptr << endl; // Undefined behavior return 0; }
The above program produces the following result:
Output of this program will be garbage address.
Dangling Pointer Due to Memory De-allocation
De-allocation of memory occurs when allocated memory is freed, causing pointers to that memory to become invalid or dangling.
int main() { float *p = (float *)malloc(sizeof(float)); //dynamic memory allocation. free(p); //after calling free() p becomes a dangling pointer p = NULL; //now p no more a dangling pointer. }
Dangling Pointer Due to Variable Going Out of Scope
A pointer to a local variable becomes dangling when the variable goes out of scope, as its memory is deallocated.
int main() { int *p //some code// { int c; p=&c; } //some code// //p is dangling pointer here. }
Void Pointer
Void pointer is a pointer which is not associate with any data types. It points to some data location in storage means points to the address of variables. It is also called general purpose pointer.
Syntax
Following is the syntax to the void pointer:
void *ptr;
It has some limitations:
- Pointer arithmetic is not possible of void pointer due to its concrete size.
- It can't be used as dereferenced.
Example
In this example, we use void pointer to store and typecast the addresses of an integer and a float.
#include<stdlib.h> #include<iostream> using namespace std; int main() { int a = 7; float b = 7.6; void *p; p = &a; cout<<*( (int*) p)<<endl ; p = &b; cout<< *( (float*) p) ; return 0; }
The above program produces the following result:
7 7.600000
Null Pointer
Null pointer is a pointer which points nothing.
Syntax
Following is the syntax to the null pointer:
int *ptr = nullptr;
Some uses of null pointer are:
-
To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet.
-
To pass a null pointer to a function argument if we don't want to pass any valid memory address.
-
To check for null pointer before accessing any pointer variable. So that, we can perform error handling in pointer related code e.g. dereference pointer variable only if it's not NULL.
Example
In this example, we initialize a pointer to NULL which indicates it to no valid memory.
#include <iostream> using namespace std; int main() { int *p= NULL; //initialize the pointer as null. cout<<"The value of pointer is "; cout<<p; return 0; }
The above program produces the following result:
The value of pointer is 0.
Wild Pointer
Wild pointers are pointers those are point to some arbitrary memory location. (not even NULL)
Syntax
Following is the syntax to the wild pointer:
int *ptr; // wild pointer (not initialized) *ptr = 5;
Example
In this example, we use a wild pointer without initialization, which causes undefined behavior.
#include <iostream> using namespace std; int main() { int *ptr; // wild pointer *ptr = 5; // may crash or corrupt memory return 0; }
The above program produces the following result:
(Segmentation fault or unexpected result)