← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 11 of 17

C++ का reinterpret_cast

reinterpret_cast computer को कुछ memory को एक बिल्कुल अलग type की तरह treat करने को कहता है, एक book को एक map की तरह पढ़ने जैसा। यह powerful और risky दोनों है।
Syntax
cpp
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?

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

Function Pointers Cast करना

आप एक function pointer type को दूसरे में convert करने के लिए reinterpret_cast इस्तेमाल कर सकते हैं। यह कभी-कभी system programming में एक single array के अंदर diverse function pointers store करने के लिए इस्तेमाल होता है।

उदाहरण: Casting Function Pointers

cpp
#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;
}

Security और Portability Risks

क्योंकि reinterpret_cast C++ type safety को पूरी तरह bypass करता है, यह highly non-portable और risky है। गलत इस्तेमाल होने पर यह आसानी से undefined behavior या memory access errors की ओर ले जा सकता है।

उदाहरण: Security and Portability Risks

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. unrelated types के बीच reinterpret_cast इस्तेमाल करना और data को valid मान कर पढ़ना।
  2. यह मान लेना कि result platforms के आर-पार portable है।
  3. result dereference करना और undefined behavior cause करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.