Null pointers c++ In Hindi
C++ प्रोग्रामिंग लैंग्वेज में नल पॉइंटर एक यूजर डिफाइन ऐरे डाटा एलिमेंट ऐसा पॉइंटर लोकेशन होता है, जो किसी प्रोग्राम में डिक्लेअर वैलिड वेरिएबल मेमोरी एड्रेस को इंडीकेट नहीं करता है। नल पॉइंटर का यूज़ सामान्य रूप से यह इंडीकेट करने में किया जाता है कि पॉइंटर या तो अनइनिशियलाइज़्ड आर्डर में है, या क्लियर आर्डर में किसी वैलिड ऐरे वेरिएबल एलिमेंट मेमोरी लोकेशन को रेफर न करने के लिए सेट किया गया है। C++ प्रोगाम में नल पॉइंटर एरर लोकेशन को चेक करने, इनवैलिड मेमोरी को डीरेफ़रेंस अवॉयड करने और डायनामिक मेमोरी को मैनेज करने में हेल्प करते हैं।

So, let’s explore the null pointer concept in C++ programming.
What is a null pointer in C++?
C++ में नल पॉइंटर एक ऐसा सिस्टम डिफाइन पॉइंटर एड्रेस लोकेशन होता है, जो किसी वैलिड मेमोरी एलिमेंट वेरिएबल स्टोरेज लोकेशन को इंडीकेट नहीं करता है। C++ में नल पॉइंटर का यूज़ सामान्य रूप से किसी एलिमेंट वेरिएबल की वैल्यू की गैर-मौजूदगी को रिप्रेजेंट करने या पॉइंटर को अनइनिशियलाइज़्ड आर्डर में मार्क करने में किया जाता है।
A null pointer in C++ means.
- C++ प्रोग्राम में नल पॉइंटर कोई वैलिड मेमोरी एड्रेस लोकेशन नहीं है।
- सामान्य रूप से, नल पॉइंटर की वैल्यू 0 या nullptr जैसा कोई कॉन्स्टेंट (मॉडर्न C++ में) एन्ड ऑफ़ द ऐरे ब्लॉक या नल एक ऐसी लास्ट वैल्यू है. जहा कोई स्टोरेज एलिमेंट एड्रेस नहीं होती है।
- ओल्ड C++ प्रोग्राम सोर्स कोड (C++11 वर्जन से पहले) में, नल पॉइंटर ऑब्जेक्ट को डिस्प्ले करने के लिए मैक्रो NULL स्टेटमेंट का यूज़ किया जाता था. लेकिन C++11 और आज के वर्जन में, nullptr का यूज़ करना ज़्यादा प्रेफर किया जाता है।
How to initialize a null pointer in a C++ program.
Using nullptr in a C++ program (in C++11 and later versions).
int* pntr = nullptr; // C++11 और बाद के नए वर्जन में आप ऐसे नल पॉइंटर को डिफाइन कर सकते है
Using the NULL text statement (in versions before C++11).
int* pntr = NULL; // ओल्ड C++ स्टैंडर्ड वर्जन में आप नल को इस तरह डिफाइन कर सकते है.
Using 0 (in older C++ standard versions) for null.
int* pntr = 0; // बहुत पुराने C++ स्टैंडर्ड वर्जन में हम नल पॉइंटर को कुछ इस तरह डिफाइन करते थे.
Explicitly setting pointer data to null.
int* pntr = nullptr; // यह पॉइंटर डाटा को एक नल पर सेट करने का स्टैंडर्ड तरीका है.
Checking for a Null Pointer in C++ Programming.
किसी C++ प्रोग्राम में अनडिफाइंड बिहेवियर (जैसे, सेगमेंटेशन फॉल्ट) को अवॉयड करने के लिए, पॉइंटर डाटा को डीरेफ़रेंस करने से पहले यह चेक करना ज़रूरी है कि वह नल डिफाइन वैल्यू है, या नहीं है। यहाँ आप C++ में if स्टेटमेंट का यूज़ करके यह चेक कर सकते हैं, कि इसमें डिफाइन पॉइंटर नल है, या नहीं है।
Example of checking for a Null Pointer.
#include <iostream>
int main() {
int* pntr = nullptr; // here we initialize pointer to as null value
// here we check if the pointer is null or not null
if (pntr == nullptr) {
std::cout << “Pointer value is null.” << std::endl;
} else {
std::cout << “Pointer value is not null.” << std::endl;
}
return 0;
}
Explanation of Checking for a Null Pointer
- यहाँ इस एक्साम्प्ल में पॉइंटर वैल्यू null डिफाइन की गई है.
- यहाँ pntr nullptr डिफाइन किया गया है, इस वजह से यह चेक if (pntr == nullptr) true आउटपुट स्टेटमेंट होता है, और प्रोग्राम में “Pointer value is null” स्टेटमेंट को प्रिंट करता है.
Dereferencing Null Pointers in a C++ Program.
किसी C++ प्रोग्राम में एक नल पॉइंटर को डीरेफ़रेंस करना (जिस मेमोरी लोकेशन को वह पॉइंट करता है, उसे एक्सेस करने की कोशिश करना) अनडिफ़ाइंड नल पॉइंटर बिहेवियर का आउटपुट होता है. जिससे सामान्य रूप में कोई प्रोग्राम क्रैश हो जाता है. प्रोग्राम में होने वाले (सेगमेंटेशन फ़ॉल्ट या एक्सेस वायलेशन) के वजह यह होता है.
Example of invalid dereferencing in C++.
#include <iostream>
int main() {
int* pntr = nullptr; // here we initialize pointer as null value
// here it attempting to dereference a null pointer value
// here this will cause undefined null pointer behavior (Segmentation fault (core dumped))
std::cout << *pntr << std::endl; // it play a undefined program behavior
return 0;
}
Using Null Pointers in Dynamic Memory Allocation in C++ Programming.
C++ प्रोग्रामिंग में डायनामिक मेमोरी एलोकेशन में, C++ यूजर यह चेक करके चेक कर सकते हैं कि मौजूदा प्रोग्राम में मेमोरी एलिमेंट डाटा टाइप का एलोकेशन सक्सेस हुआ या नहीं है, यह चेक करके कि new कीवर्ड या malloc फंक्शन द्वारा रिटर्न किया गया पॉइंटर nullptr है, या नहीं है।
Example of the new method with a null pointer check.
#include <iostream>
int main() {
int* pntr = nullptr;
// here we dynamically allocate memory for one integer data type
pntr = new(std::nothrow) int; // here we define new(std::nothrow) returns nullptr if allocation fails
if (pntr == nullptr) {
std::cout << “New Pointer Int data Memory allotment failed.” << std::endl;
} else {
std::cout << “New Pointer Int data Memory allotment succeeded.” << std::endl;
delete pntr; // here you don’t forget to free allocated int data type memory storage block
}
return 0;
}
Explanation of the new method with a null pointer check.
- यहाँ इस एक्साम्प्ल में new(std::nothrow) int एक इंटीजर डाटा टाइप के लिए मेमोरी एलोकेशन करने की कोशिश करता है, लेकिन इसमें मेमोरी एलोकेशन टास्क फेल हो जाता है. जैसे, मेमोरी की कमी के कारण, तो यह nullptr वैल्यू को रिटर्न करता है।
- इसके बाद पॉइंटर pntr को यूज़ करने से पहले यह देखने के लिए चेक किया जाता है कि यह null हुआ है, या नहीं है।
Detail Summary of Null Pointer Concepts in c++
| Null Pointer Concept | Null Pointer Description |
| What is Null Pointer | Null pointer used as pointer that does not point to any valid storage element memory location. |
| How to Initialization | We can define it as int* pntr = nullptr; (C++11 and later version) or int* pntr = NULL; |
| What is Dereferencing | We can dereferencing a null pointer results in undefined program output behavior. |
| Null Pointer Checking | If we need, we can always check if a pointer is null before dereferencing it or not. |
| Dynamic Memory Allocation | We can use new and malloc keyword or function to return nullptr if memory allocation task fails. |

