Memory leaks and how to avoid them c++ In Hindi

Memory leaks and how to avoid them c++ In Hindi

C++ प्रोग्रामिंग लैंग्वेज में मेमोरी लीक कंडीशन तब जनरेट होती है, जब किसी C++ प्रोग्राम में डायनामिक आर्डर में एलोकेट अलॉट की गई प्रोग्राम मेमोरी टाइप को प्रॉपर आर्डर में डी-एलोकेट नहीं किया जाता है. जिससे की मौजूदा प्रोग्राम उसके मेमोरी ट्रैक को लॉस्ट कर देता है, और प्रोगाम में मेमोरी एलोकेशन यथावत बनी रहती है. सामान्य रूप से मौजूदा प्रोग्राम के समाप्त होने तक। इस वजह से मौजूदा प्रोग्राम में मेमोरी का प्रॉपर यूज़ नहीं हो पाता है, और धीरे धीरे समय के साथ, इसमें प्रोग्राम की मेमोरी अपने आप समाप्त हो सकती है. किसी स्पेशल कंडीशन में लंबे समय तक रन होने वाले प्रोग्राम या सॉफ्टवेयर एप्लिकेशन में ये मौजूद बनी रहती है।

Memory leaks and how to avoid them c++ In Hindi

Example of a memory leak in C++ programming.

#include <iostream>

int main() {

int* ptr = new int(17); // Here, memory is allocated dynamically for integer data types variable.

// Here, we have not used any delete statements, resulting in a memory leak.

return 0;

}

Explanation of a memory leak in C++ programming.

  • यहाँ ऊपर दिए गए एक्साम्प्ल में new ऑपरेटर एक int डाटा टाइप के लिए इन्टिजर मेमोरी को अलॉट एलोकेट करता है. लेकिन इस प्रोग्राम में मौजूदा मेमोरी को फ्री करने के लिए कोई डिलीट ऑपरेटर यूज़ नहीं होता है। इसके परिणाम स्वरुप, प्रोग्राम में एलोकेट की गई वेरिएबल पैरामीटर मेमोरी “लीक” हो जाती है।

Causes of C++ Programming Memory Leaks.

किसी C++ प्रोग्राम में मेमोरी लीक एक्शन सामान्य रूप से नीचे दिए गए कुछ मुख्य कारणों में से किसी एक की वजह से जनरेट होते हैं.

Failure to deallocate allocated memory in a C++ program.

कई बार किसी C++ प्रोग्राम में अलॉट की गई पैरामीटर वेरिएबल मेमोरी की ज़रूरत न रहने के बाद हम उसे डी-एलोकेट करना भूल जाते है।

Overwriting pointers in a C++ program.

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

int* ptr = new int(8);

ptr = new int(19); // Here, the memory previously allocated in the `ptr` variable is lost.

// And the original variable memory defined in it is leaked into the program.

C++ Program Memory Exception.

यदि C++ यूजर डिफाइन किसी प्रोग्राम में मेमोरी एलोकेट होने के बाद लेकिन फ्री होने से पहले कोई एक्सेप्शन डिस्प्ले करता है. तो इस कंडीशन में मौजूदा प्रोग्राम में मेमोरी को डी-एलोकेट नहीं किया जा सकता है।

void test() {

int* ptr = new int(11);

throw std::runtime_error(“A memory exception error displayed”); // Memory is never freed in this situtation

}

Not freeing dynamically allocated arrays in a C++ program.

किसी C++ ऐरे प्रोग्राम में new[] स्क्वायर ब्रैकेट ब्लॉक के साथ एलोकेट किए गए एरे डाटा टाइप मेमोरी के लिए delete[] ऐरे मेथड का यूज़ न करने से मेमोरी लीक हो सकती है।

int* array = new int[19]; ​​// Here we have allocated memory for an array variable named integer.

// Missing the delete[] array method in this condition can lead to a memory leak.

How to avoid memory leaks in C++ programming.

Manually free program memory when necessary.

सबसे पहले आपके यूज़ होने वाले प्रोग्राम में यह सुनिश्चित करें कि हर नए या new[] ऐरे ब्लॉक के साथ एक मिलता-जुलता डिलीट या delete[] मेथड जरूर अप्लाई किया गया हो।

For freeing a single memory object.

int* ptr = new int(13);

// Here the ptr variable is used to allocate memory

delete ptr; // Here the delete keyword is used to free the allocated memory

To free memory for the sequence array data type.

int* array = new int[27];

// Here the integer array is used to allocate a 27 array memory block

delete[] array; // This uses the existing array to free the 27 array allocated memory

C++ Programs Use Smart Pointers.

C++ प्रोग्राम में डिफाइन स्मार्ट पॉइंटर्स रॉ पॉइंटर्स के चारों तरफ से रैपर होते हैं, जो ऑटोमैटिकली किसी प्रोग्राम में मेमोरी को मैनेज करते हैं, और जब मौजूदा प्रोग्राम में इनकी ज़रूरत न हो तो इसे डीलोकेट भी कर देते हैं।

C++ Standard Library provides several types of smart pointers to its users.

std::unique_ptr – C++ प्रोग्राम में यह एक स्मार्ट पॉइंटर वेरिएबल होते है, जो किसी प्रोग्राम मेमोरी रिसोर्स की पूरी ओनरशिप कण्ट्रोल प्रोवाइड करता है, और जब वह उसके स्कोप से बाहर हो जाता है. तो यह डिक्लेअर स्मार्ट पॉइंटर वेरिएबल की मेमोरी को ऑटोमैटिकली डीलोकेट कर देता है। न ही प्रोग्राम में इसे कॉपी किया जा सकता है, लेकिन जरूरत पड़ने पर इसे मूव जरूर किया जा सकता है।

#include <memory>

void test() {

std::unique_ptr<int> ptr = std::make_unique<int>(13);

// There is no need to manually delete ptr in this program; it is a smart pointer and will be deleted automatically.

}

std::shared_ptr – C++ प्रोग्राम में यह एक रेफरेंस-काउंटेड स्मार्ट पॉइंटर वेरिएबल है। इसमें कई shared_ptr इंस्टेंस एक ही प्रोग्राम ऑब्जेक्ट की ओनरशिप को शेयर कर सकते हैं। जब लास्ट shared_ptr वेरिएबल इसके स्कोप से बाहर हो जाता है, या रीसेट हो जाता है, तो इस प्रोग्राम में अलॉट ptr मेमोरी अपने आप ही डी-एलोकेट हो जाती है।

#include <memory>

void test() {

std::shared_ptr<int> ptrone = std::make_shared<int>(19);

std::shared_ptr<int> ptrtwo = ptrone; // This is an example of shared ptr ownership.

// You don’t need to delete the ptr here; the memory allocated to it is automatically freed when both ptrone and ptrtwo exit their scope.

}

std::weak_ptr – C++ प्रोग्राम में एक शेयर्ड_ptr द्वारा मैनेज किए जाने वाले प्रोग्राम ऑब्जेक्ट का एक नॉन-ओनिंग रेफरेंस होता है। और इसका यूज़ एक शेयर्ड_ptrs के बीच सर्कुलर रेफरेंस को अवॉयड करने में किया जाता है।

std::shared_ptr<int> shared = std::make_shared<int>(21);

std::easy_ptr<int> easy= shared; // यहाँ यूजर डिफाइन easy_ptr मेमोरी डीलोकेशन को नहीं रोकता है

Use RAII (Resource Acquisition Is Initialization) in C++.

C++ प्रोग्राम में RAII एक डिज़ाइन पैटर्न मेथड है, जहाँ यह प्रोग्राम में सिस्टम रिसोर्स (जैसे वेरिएबल मेमोरी) ऑब्जेक्ट के लाइफटाइम से कनेक्टेड होते हैं। जब मौजूदा प्रोग्राम में ऑब्जेक्ट इसके स्कोप से बाहर हो जाता है. तो इसमें डिस्ट्रक्टर मेथड को कॉल किया जाता है, और सिस्टम अपने मेमोरी रिसोर्स को क्लीन अप कर देता है।

Example with RAII in a C++ program.

class System_data {

    public:

        System_data() { ptr = new int(27); }

        ~System_data() { delete ptr; }  // here we use destructor method to frees its memory

    private:

        int* ptr;

    };

    void test() {

        System_data s; // here memory allocated in constructor, automatically freed in destructor method

    }

Explanation with RAII in a C++ program.

  • यहाँ इस एक्साम्प्ल में, जब System_data ऑब्जेक्ट इसके स्कोप से बाहर हो जाता है. तो सिस्टम में अलॉट मेमोरी अपने आप फ्री हो जाती है, मौजूदा प्रोग्राम में डिफाइन डिस्ट्रक्टर मेथड की वजह से।

Leave a Reply