reinterpret_cast in C++
Reinterpret_cast is one of the four casting operators available in C++ for converting between distinct types. It is the most mighty and hazardous because it enables low-level type conversions that may not be safe or useful. The reinterpret_cast is often used when you need to handle an object or a block of memory as if it were of a different type. Still, it should be used with caution and only when you are certain you understand what you are doing, as it can result in undefined behavior if used incorrectly.
Syntax:
Here is the basic syntax of reinterpret_cast:
new_type expression new_type is the type of object to which you wish to cast the expression.
The value or variable to be cast is specified via expression.
Here's a demonstration of how reinterpret_cast may be used:
int num = 47; double* ptr = reinterpret_cast(&num);
In this example, we will interpret the memory held by an int (num) as a double pointer. It is a problematic operation since int and double have distinct memory layouts, and referring to ptr in this situation would result in uncertain behavior.
Low-level tasks such as casting between pointers and integers (e.g., for memory management) or manipulating platform-specific data structures are common uses for reinterpret_cast. It's also used when interfacing with C code or hardware interfaces.
Example:
Let's take an example to demonstrate the reinterpret_cast in C++:
Filename: pointer.cpp
// CPP program to illustrate how // reinterpret_cast works #includeusing namespace std; int main() { int* pt = new int(80); char* cha = reinterpret_cast (pt); cout << *pt << endl; cout << *cha << endl; cout << pt << endl; cout << cha << endl; return 0; }
Output:
80 P 0x119beb0 P
The goal of using reinterpret_cast:
- The reinterpret_cast is a unique and unsafe casting operator. It is also recommended to use it with the right data type, i.e. (the pointer data type must be the same as the original data type).
- It may typecast any pointer to any other data type.
- It is useful when you need to operate with bits.
- If you use this method of casting, the product becomes non-portable. As a result, it is advised that this idea be avoided unless it is essential.
- It is only used to typecast every pointer back to its original type.
- A Boolean value will be translated into an integer value, i.e., 0 for false and 1 for true.
Example 2:
// CPP code to demonstrate the use of structure #includeusing namespace std; // making a structure mystruct struct mystructs { int x1; int y1; char ch; bool bo; }; int main() { mystructs ss; // the variables are assigned ss.x1 = 23; ss.y1 = 20; ss.ch = 'b'; ss.bo = true; //When casting, the data type must be the same as the original. // Converting this pointer to an int pointer in 'p1'. int* p1 = reinterpret_cast (&ss); cout << sizeof(ss) << endl; // displaying the value indicated by *p cout << *p1 << endl; // increasing the pointer by one. p1++; // displaying the next integer number cout << *p1 << endl; p1++; //char *ch is used to cast back the char * referred to by p1. char* cha= reinterpret_cast (p1); //displaying the character value indicated by (*ch) cout << *cha << endl; cha++; /* Because (*cha) now points to a boolean value, retrieving the value via the same type conversion is necessary. As a result, we changed the data type of *num to bool.*/ bool* num = reinterpret_cast (cha); cout << *num << endl; // We may also use this line of code to output the value indicated by (*cha). cout << *(reinterpret_cast (cha)); return 0; }
Output:
12 23 20 b 1 1
Example 3:
// CPP example code for the pointer reinterpret #includeusing namespace std; class A1 { public: void function_a() { cout << " The class A "; } }; class B1 { public: void function_b() { cout << " The class B "; } }; int main() { //an object of the class B B1* x = new B1(); // pointer to object conversion: // references of class B to class A A1* newobject_a = reinterpret_cast (x); //using class A's method to access newobject_a->function_a(); return 0; }
Output:
The class A