C++ का nullptr
In this page:
data_type *pointer_name = nullptr;
if (pointer_name == nullptr) {
// points to nothing
}
nullptr क्या है?
nullptr C++11 में introduce हुआ एक dedicated null-pointer literal है जिसका अपना type है, std::nullptr_t, जो implicitly किसी भी pointer type में convert होता है लेकिन एक integer में नहीं।
यह C की convention से bugs का एक लंबे समय से चला आ रहा source बंद करता है जो "no pointer" मतलब integer 0 इस्तेमाल करती थी।
उदाहरण: What is nullptr?
// 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 == nullptr) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
nullptr बनाम NULL
C और पुराने C++ में, NULL typically बस integer literal 0 के लिए एक macro है, जिसका मतलब है compiler हमेशा नहीं बता सकता कि आपका मतलब एक null pointer था या number zero -- यह ambiguity overloaded functions में एक real problem बन जाती है जो एक pointer और एक int parameter दोनों accept करते हैं।
उदाहरण: nullptr vs. NULL
// Include std::cout and std::cin
#include <iostream>
#include <cstddef>
// Program execution starts in main()
int main() {
int *ptr1 = nullptr;
int *ptr2 = NULL;
// Print to the console with cout
std::cout << (ptr1 == ptr2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
nullptr के साथ Overloading
क्योंकि nullptr का अपना pointer-only type है, इसे एक overloaded function को pass करना unambiguously pointer overload select करता है, जबकि same overloaded functions को NULL (जो actually बस 0 है) pass करना गलती से integer version call कर देगा -- एक classic C++98 gotcha जिसे fix करने के लिए nullptr specifically design हुआ।
उदाहरण: Overloading with nullptr
// Include std::cout and std::cin
#include <iostream>
void show(int x) { std::cout << "int: " << x << std::endl; }
void show(char *p) { std::cout << "pointer" << std::endl; }
// Program execution starts in main()
int main() {
show(nullptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Validity Check करना
एक null pointer dereference करना -- ptr के nullptr होने पर *ptr से पढ़ना या लिखना -- एक segmentation fault cause करता है जो program को तुरंत crash कर देता है।
Dereference करने से पहले if (ptr != nullptr) (या बस if (ptr)) check करना इसके against standard guard है।
उदाहरण: Checking Pointer Validity
// 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 << "Pointer is null, skipping dereference" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions को nullptr Pass करना
nullptr को एक function argument की तरह pass करना एक optional pointer parameter के लिए "कोई value नहीं दी गई" signal करने का एक clear, type-safe तरीका है, function को एक separate boolean flag की ज़रूरत के बिना यह branch करने देते हुए कि real data supply हुआ या नहीं।
उदाहरण: Passing nullptr to Functions
// Include std::cout and std::cin
#include <iostream>
void process(int *data) {
// Check whether data == nullptr
if (data == nullptr) {
// Print to the console with cout
std::cout << "No value provided" << std::endl;
} else {
// Print to the console with cout
std::cout << *data << std::endl;
}
}
// Program execution starts in main()
int main() {
process(nullptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
nullptrकी बजायNULLया0इस्तेमाल करना, जो गलत overload select कर सकता है।- बिना check किए एक null pointer dereference करना।
- यह मान लेना कि
nullptrको एकintमें assign किया जा सकता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: