Dangling Pointer डेंगलिंग पॉइंटर

A pointer which initially stores the memory address of an object or variable, but after the object is destroyed or the memory is deallocated, the pointer still retains the same address. Such a pointer is called a dangling pointer.

सी प्रोग्राम में एक पॉइंटर वेरिएबल जो प्रारंभ में किसी ऑब्जेक्ट या वेरिएबल का मेमोरी एड्रेस स्टोर करता है, परंतु बाद में उस ऑब्जेक्ट के नष्ट हो जाने या मेमोरी डी-एलोकेट हो जाने के बाद भी पॉइंटर में वही पुराना एड्रेस स्टोर रहता है। इस प्रकार के पॉइंटर को डेंगलिंग पॉइंटर (Dangling Pointer) कहा जाता है।

A dangling pointer refers to memory that is no longer valid for the original object. If the programmer tries to access or modify the value through this pointer, it results in undefined behavior. It may cause program crashes, unexpected output, or data corruption.

डेंगलिंग पॉइंटर ऐसी मेमोरी को पॉइंट करता है जो मूल ऑब्जेक्ट के लिए अब वैलिड नहीं होती है। यदि प्रोग्रामर इस पॉइंटर के द्वारा वैल्यू को एक्सेस या मॉडिफाई करने का प्रयास करता है, तो यह अनडिफाइंड बिहेवियर उत्पन्न करता है। इससे प्रोग्राम क्रैश हो सकता है, अनएक्सपेक्टेड आउटपुट प्राप्त हो सकता है या डेटा करप्ट हो सकता है।

Example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *p;

    p = (int *)malloc(sizeof(int));

    *p = 10;

    printf("Value = %d\n", *p);

    free(p);

    // Pointer p now becomes dangling

    return 0;
}

Explanation:

In the above program:

  • malloc() allocates memory for an integer variable.

  • Pointer p stores the address of the allocated memory.

  • *p = 10; stores the value 10 in the allocated memory.

  • free(p); deallocates the memory.

  • After free(p);, pointer p still contains the old memory address, but the memory is no longer valid for accessing the original object.

उपरोक्त प्रोग्राम में:

  • malloc() एक इन्टिजर के लिए मेमोरी एलोकेट करता है।

  • पॉइंटर p एलोकेटेड मेमोरी का एड्रेस स्टोर करता है।

  • *p = 10; एलोकेटेड मेमोरी में 10 वैल्यू स्टोर करता है।

  • free(p); मेमोरी को डी-एलोकेट करता है।

  • free(p); के बाद भी पॉइंटर p में पुराना मेमोरी एड्रेस स्टोर रहता है, परंतु वह मेमोरी अब मूल ऑब्जेक्ट को एक्सेस करने के लिए वैलिड नहीं होती है।

Important Note:

To avoid dangling pointers, programmers should assign NULL to a pointer after freeing the memory when appropriate.

free(p);
p = NULL;

मेमोरी को फ्री करने के बाद डेंगलिंग पॉइंटर से बचने के लिए आवश्यकतानुसार पॉइंटर को NULL असाइन करना चाहिए।

Causes of Dangling Pointer:

  1. Deallocating dynamically allocated memory using free().

  2. Returning the address of a local variable from a function.

  3. Accessing an object after its lifetime has ended.

डेंगलिंग पॉइंटर के कारण:

  1. free() के द्वारा डायनामिकली एलोकेटेड मेमोरी को डी-एलोकेट करना।

  2. किसी फंक्शन से लोकल वेरिएबल का एड्रेस रिटर्न करना।

  3. किसी ऑब्जेक्ट की लाइफटाइम समाप्त होने के बाद उसे एक्सेस करना।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Top High-Paying Tech Skills to Learn in 2025

Complete C Programming Syllabus | Advanced C Roadmap for Students