← Back to C++ Course | Chapter 16: Modern C++ | Lesson 3 of 9

C++ का nullptr

nullptr यह कहने का clear, modern तरीका है कि एक pointer कहीं point नहीं करता, एक envelope पर एक empty address की तरह। यह confusing पुराने NULL की जगह लेता है।
Syntax
cpp
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?

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

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

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

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

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

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

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

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

cpp
// 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;
}
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. nullptr की बजाय NULL या 0 इस्तेमाल करना, जो गलत overload select कर सकता है।
  2. बिना check किए एक null pointer dereference करना।
  3. यह मान लेना कि nullptr को एक int में assign किया जा सकता है।

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.