C++ के Null और Void Pointer
In this page:
data_type *pointer_name = nullptr;
if (pointer_name != nullptr) {
// safe to use *pointer_name
}
Null Pointer क्या है?
एक null pointer एक pointer है जो जानबूझकर किसी valid memory address की ओर point नहीं करता, 'यहाँ अभी कोई object नहीं' represent करने के लिए इस्तेमाल होता है।
Modern C++ इसके लिए पुराने NULL macro की बजाय nullptr keyword इस्तेमाल करता है, क्योंकि nullptr का अपना type है और इसे integer 0 से चुपचाप confuse नहीं किया जा सकता।
उदाहरण: What is a Null Pointer?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int *ptr = nullptr;
// Print to the console with cout
std::cout << "ptr points to nothing yet" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Null Check करना
इसे dereference करने से पहले हमेशा check करें कि एक pointer null है या नहीं, क्योंकि एक null pointer से पढ़ना या इसमें लिखना undefined behavior है और typically आपके program को तुरंत एक segmentation fault से crash कर देता है।
यह एक habit pointer-heavy code में सबसे common runtime bugs में से एक रोकती है।
उदाहरण: Checking for Null
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int *ptr = nullptr;
// Check whether ptr != nullptr
if (ptr != nullptr) {
// Print to the console with cout
std::cout << *ptr << std::endl;
} else {
// Print to the console with cout
std::cout << "Cannot dereference: pointer is null" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Void Pointer क्या है?
एक void pointer (void*) एक generic pointer है जो किसी भी data type के variable का address hold कर सकता है, जो इसे type-agnostic low-level utilities लिखने के लिए useful बनाता है।
हालांकि, क्योंकि compiler अब नहीं जानता यह किस type की ओर point करता है, आप एक void pointer को सीधे dereference नहीं कर सकते।
उदाहरण: What is a Void Pointer?
#include <iostream>
int main() {
int num = 5;
void *vptr = # // can hold the address of any type
std::cout << "Void pointer set" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Void Pointers Cast करना
Actually एक void pointer के refer किए data को पढ़ने के लिए, आपको पहले इसे वापस इसके real, specific type में cast करना होगा, आमतौर पर modern C++ में static_cast से।
यह step skip करना और void pointer को as-is dereference करना compile भी नहीं होगा, क्योंकि compiler को नहीं पता कितने bytes पढ़ने हैं।
उदाहरण: Casting Void Pointers
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare num and set it to 42
int num = 42;
void *vptr = #
int *intPtr = static_cast<int *>(vptr);
// Print to the console with cout
std::cout << *intPtr << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Common Pitfalls
एक void pointer को गलत type में cast करने से बचें, क्योंकि यह program को एक incorrect size और layout इस्तेमाल करके memory reinterpret कराता है, garbage values या corruption produce करते हुए।
यह भी कभी void या null pointer पर पहले check या cast किए बिना सीधे dereferencing syntax apply न करें।
उदाहरण: Common Pitfalls
#include <iostream>
int main() {
double num = 3.14;
void *vptr = #
int *wrongPtr = static_cast<int *>(vptr); // wrong type: misreads the bytes
std::cout << "Casting to the wrong type corrupts the value read" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक null pointer dereference करना, जो program crash करता है।
- जहाँ
nullptrsafer है वहाँNULLया0इस्तेमाल करना, जो गलत overload चुन सकता है। - एक
void*को सीधे dereference करना, जब इसे पहले एक specific pointer type में cast करना ज़रूरी है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: