
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
Is It Safe to Delete a Void Pointer in C/C++?
The 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.
Is It Safe to Delete a Void Pointer in C/C++?
It is not safe to delete a void pointer in C/C++ because delete needs to call the destructor of whatever object it's destroying, and it is impossible to do that if it doesn't know the type.
C Example of Void Pointer
In this example, a void pointer (void *p) can store the address of any data type, but it must be explicitly typecast before dereferencing, which can be unsafe. Here, we first assign an integer to the pointer and then assign a float. The typecasting is performed very easily, and we display the result.
#include<stdio.h> #include<stdlib.h> int main() { int a = 7; float b = 7.6; void *p; p = &a; printf("Integer variable is = %d", *( (int*) p) ); // It works but implicitly p = &b; printf("\nFloat variable is = %f", *( (float*) p) ); return 0; }
The above code produces the following result:
Integer variable is = 7 Float variable is = 7.600000
C++ Example of Void Pointer
In this example, we are using a void* pointer which is very similar to C program, but C++ has a additional feature like static_cast for type safety conversion.
#include<iostream> using namespace std; int main() { int a = 7; float b = 7.6; void* p; p = &a; cout << "Integer variable is = " << *(static_cast<int*>(p)) << endl; p = &b; cout << "Float variable is = " << *(static_cast<float*>(p)) << endl; return 0; }
The above code produces the following result:
Integer variable is = 7 Float variable is = 7.6
Note: static_cast is a type conversion operator in C++ that is used for safe and explicit casting between related types at compile time.
In C, you can use void*, and it works good with memory management. In C++, you need to explicitly cast it before deleting, because the language requires strict type safety.
How to Delete a Void Pointer Safely in C++?
We can safely delete a void pointer in C++ by casting it back to its original type before using the delete operator.
Example
This example shows that deleting a void pointer is allowed , but it must point to memory allocated with new:
#include<iostream> using namespace std; int main() { void* ptr = new int(42); // dynamically allocate an int and assign to void pointer cout << "Value: " << *(static_cast<int*>(ptr)) << endl; delete static_cast<int*>(ptr); // must cast back to correct type before deleting return 0; }
The above code produces the following result:
Value: 42
Note: In C++, you can safely delete a void pointer, but it must be cast to its original type to ensure the correct destructor (if any) is called. In C, delete
is not used; memory is freed using free()
.