C++ का reinterpret_cast
In this page:
target_type *pointer_name = reinterpret_cast<target_type*>(source_pointer);
long address = reinterpret_cast<long>(pointer_name);
reinterpret_cast क्या है?
reinterpret_cast operator low-level, binary-level type conversions perform करता है। यह एक object का raw bit pattern एक बिल्कुल अलग type जैसा treat करता है, data convert किए बिना।
उदाहरण: What is reinterpret_cast?
#include <iostream>
int main() {
int x = 65;
char *p = reinterpret_cast<char *>(&x); // treats raw bits as a different type
std::cout << *p << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer से Integer Conversions
आप एक memory address (a pointer) को एक integer value में convert करने के लिए reinterpret_cast इस्तेमाल कर सकते हैं। यह low-level logging या system-level debugging के लिए useful है।
static_cast के उलट, reinterpret_cast कोई safety checks या value conversion perform नहीं करता, यह बस same bits को अलग तरीके से reinterpret करता है।
उदाहरण: Pointer to Integer Conversions
#include <iostream>
int main() {
int x = 5;
int *ptr = &x;
long address = reinterpret_cast<long>(ptr); // pointer to integer
std::cout << "Address as number: " << address << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Byte Streams के साथ काम करना
Reinterpret_cast अक्सर structured data (जैसे एक struct) को एक raw byte stream (char) में convert करने के लिए इस्तेमाल होता है। यह networks पर data serialize और send करने के लिए ज़रूरी है।
क्योंकि यह type safety को पूरी तरह bypass करता है, resulting byte layout platform-dependent है और इसे real caution के साथ इस्तेमाल किया जाना चाहिए।
उदाहरण: Working with Byte Streams
#include <iostream>
struct Point { int x; int y; };
int main() {
Point p = {1, 2};
char *bytes = reinterpret_cast<char *>(&p); // struct viewed as raw bytes
std::cout << (int)bytes[0] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Pointers Cast करना
आप एक function pointer type को दूसरे में convert करने के लिए reinterpret_cast इस्तेमाल कर सकते हैं। यह कभी-कभी system programming में एक single array के अंदर diverse function pointers store करने के लिए इस्तेमाल होता है।
उदाहरण: Casting Function Pointers
#include <iostream>
void hello() { std::cout << "Hello" << std::endl; }
int main() {
void *generic = reinterpret_cast<void *>(hello);
void (*fn)() = reinterpret_cast<void (*)()>(generic); // back to a function pointer
fn();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Security और Portability Risks
क्योंकि reinterpret_cast C++ type safety को पूरी तरह bypass करता है, यह highly non-portable और risky है। गलत इस्तेमाल होने पर यह आसानी से undefined behavior या memory access errors की ओर ले जा सकता है।
उदाहरण: Security and Portability Risks
#include <iostream>
int main() {
float f = 3.14f;
int *misread = reinterpret_cast<int *>(&f); // bypasses type safety entirely
std::cout << "Bit pattern misread as int: " << *misread << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- unrelated types के बीच
reinterpret_castइस्तेमाल करना और data को valid मान कर पढ़ना। - यह मान लेना कि result platforms के आर-पार portable है।
- result dereference करना और undefined behavior cause करना।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced