In C++, a void pointer is a pointer that is declared using the 'void' keyword (void*). It is different from regular pointers it is used to point to data of no specified data type. It can point to any type of data so it is also called a "Generic Pointer".
Syntax of Void Pointer in C++
void* ptr_name;
As the type of data the void pointer is pointing to is unknown, we cannot dereference a void pointer.
Example of Void Pointer in C++
The below example demonstrates the declaration and use of void pointer in C++.
C++
// C++ Program to demonstrate the declaration and use of a
// void pointer
#include <iostream>
using namespace std;
int main()
{
int a = 10;
// void pointer holds address of int 'a'
void* myptr = &a;
// printing the value of a and adress of a stored in
// myptr
cout << "The value of a is: " << a << endl;
cout << "The Adress of a is " << myptr << endl;
}
OutputThe value of a is: 10
The Adress of a is 0x7ffd9ac02a64
Application of Void Pointer in C++
1. Generic Coding
The concept of a generic pointer means a pointer that can be used to point to data of any data type. This is helpful in situations where you need a single pointer that can handle different data types dynamically.
Let's see an example for this, let's first declare variables of different types (int, double, char). then declare a void pointer named "generic pointer". We use the void pointer to point to each of the variables in succession with the help of typecasting.
Example
Below is a program in which an int type pointer is declared and we try to point the value of a float datatype using it. Let's See what happens.
C++
// C++ program to demonstrate the need for void pointer
#include <iostream>
using namespace std;
int main()
{
int* ptr;
float f = 90.6;
ptr = &f; // error
cout << "The value of *ptr is : " << *ptr << endl;
return 0;
}
Output
error: cannot convert ‘float*’ to ‘int*’ in assignment
Explanation: The above program gives an error the reason is int type pointer ptr is declared and we are trying to store the memory address of a float data type into an int pointer, which gives an error. But if we declare void pointer it allows us to change their data type. So, declare ptr as a void pointer and the program will not give any error. The below example demonstrates the same.
C++
// C++ Program to demonstrate the use of void pointer
// to hold the address of any type-castable type
#include <iostream>
using namespace std;
int main()
{
// Initializing multiple variables of different data
// type
int n = 10;
float f = 25.25;
char c = '$';
// Initializing a void pointer
void* ptr;
ptr = &n; // pointing to int
cout << "The value of n " << n << endl;
cout << "The Adress of n " << ptr << endl;
ptr = &f; // pointing to float
cout << "The value of n " << f << endl;
cout << "The Adress of n " << ptr << endl;
ptr = &c; // pointing to char
cout << "The value of n " << c << endl;
cout << "The Adress of n " << ptr << endl;
}
OutputThe value of n 10
The Adress of n 0x7ffe05023434
The value of n 25.25
The Adress of n 0x7ffe05023430
The value of n $
The Adress of n 0x7ffe0502342f
Explanation: In the above Code, We declared a void pointer as the variable name "ptr" which acts as a generic pointer or void pointer. This pointer stores multiple datatype addresses like int, char and float. We Typecasted the void pointer to any data type (float, char, int). Hence Void Pointer allows us to change the addresses for different memory addresses of different data types.
2. Dynamic Memory Allocation
Dynamic memory allocation is performed in C++, when the size of memory needed is not known at compile time then we perform Dynamic memory Allocation using operators like new or malloc. Void pointers can be used to allocate memory for any data type. In C++, "new" keyword is used for dynamic memory allocation which returns a pointer to the allocated memory. After allocating memory, We need perform type casting to use the allocated memory with a specific data type.
Example
The below program demonstrate the use of void pointer to dynamically allocate memory for any data type.
C++
// C++ program to demonstrate the use of void pointer to
// dynamically allocate memory for any data type.
#include <iostream>
using namespace std;
int main()
{
// Allocate memory for an integer using new
void* voidPtr = new int;
// Type casting the void pointer to int* for usage
int* intPtr = static_cast<int*>(voidPtr);
// Assign a value to the allocated memory
*intPtr = 42;
// Print the value from the allocated memory
cout << "Value from allocated memory: " << *intPtr
<< endl;
// Deallocate the memory
delete intPtr;
return 0;
}
OutputValue from allocated memory: 42
3. Callback Functions
Functions that are passed as arguments to other functions are called callback functions. In case we need to handle multiple data types, the void pointer can be used as a void pointer provides us the flexibility to handle different data types. Callback functions can be defined to accept a void pointer as a parameter, enabling them to handle different types of data. This approach simplifies the code because this approach maintains different data types in a single callback.
Example
The below program demonstrates the use of the void function to pass a void pointer as a parameter in a callback function.
C++
// The below program demonstrate the use of void function to
// pass void pointer as parameter in callback function.
#include <iostream>
using namespace std;
// Callback function definition
void CallbackFunction(void* data, char dataType)
{
switch (dataType) {
case 'i':
cout << "Callback for integer: " << *(int*)data
<< endl;
break;
case 'd':
cout << "Callback for double: " << *(double*)data
<< endl;
break;
default:
cout << "Unsupported data type in callback!"
<< endl;
}
}
// Function that takes a callback
void PerformOperation(void* data, char dataType,
void (*callback)(void*, char))
{
// Call the callback function
callback(data, dataType);
}
int main()
{
int intValue = 07;
double doubleValue = 8.12;
// Perform operation with integer and callback
PerformOperation(&intValue, 'i', CallbackFunction);
// Perform operation with double and callback
PerformOperation(&doubleValue, 'd', CallbackFunction);
return 0;
}
OutputCallback for integer: 7
Callback for double: 8.12
Advantages of void Pointer in C++
The void pointer have the following major applications:
- The use of void pointers can help reduce redundant code by allowing a single piece of code to handle multiple data types.
- Void pointer allows abstraction of specific types in favor of a more general interface. This is common in scenarios like polymorphic containers or callback systems.
- A void pointer can be declared to deal with polymorphism.
- Void pointers can enhance platform independence where the sizes and representations of data types may vary across different architectures.
Conclusion
In conclusion, void pointers in C++ offer flexibility and versatility in certain programming scenarios, but this demands careful consideration. Modern C++ offers safer alternatives, like smart pointers for memory management.
Similar Reads
void Pointer in C
A void pointer is a pointer that has no associated data type with it. A void pointer can hold an address of any type and can be typecasted to any type. Example of Void Pointer in C[GFGTABS] C // C Program to demonstrate that a void pointer // can hold the address of any type-castable type #include
3 min read
Smart Pointers in C++
In C++, pointers are the variables that stores the memory addresses. They are extensively used in dynamic memory location to store the address of allocated memory. But they bring a lot of issues. Problems with Normal PointersMemory Leaks: This occurs when memory is repeatedly allocated by a program
5 min read
C++ Vector of Pointers
Prerequisites Pointers in C++Vectors in C++ Vector of pointers are vectors that can hold multiple pointers. Each pointer within a vector of pointers points to an address storing a value. We can use the vector of pointers to manage values that are not stored in continuous memory. How to Create Vector
6 min read
Void Pointer in Programming
Pointer is a way of manipulating addresses in computer memory by making referenced based on its original nature rather than by its value. Void Pointer is an object that is capable of pointing to the address of any other type of data. In this article, we will discuss about what is a void pointer, how
3 min read
is_void template in C++
The std::is_void template of C++ STL is used to check whether the type is a void or not. It returns a boolean value showing the same. Syntax: template < class T > struct is_void; Parameter: This template contains single parameter T (Trait class) to check whether T is a void type or not. Return
2 min read
Function Pointer in C++
Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
Function Pointer in C
In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
list insert() in C++ STL
The list::insert() is used to insert the elements at any position of list. This function takes 3 elements, position, number of elements to insert and value to insert. If not mentioned, number of elements is default set to 1. Syntax: insert(pos_iter, ele_num, ele)Parameters: This function takes in th
2 min read
Return From Void Functions in C++
Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but the
2 min read
Pointers and References in C++
In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable. Pointers in C++ Pointers in C++ are a
5 min read