Warning: Trying to access array offset on value of type null in /home/u623081920/domains/hindilearn.in/public_html/tutorials.php on line 241

Warning: Trying to access array offset on value of type null in /home/u623081920/domains/hindilearn.in/public_html/tutorials.php on line 244
Dynamic Memory Allocation In Hindi - C++ Programming - Hindilearn

आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
C++ - Dynamic Memory Allocation

Introduction for Memory Allocation

Dynamic Memory Allocation ये c++ के लिए बहुत ही अच्छा feature है |

जब Variable declare होता है, तब variable; data type के हिसाब से Memory Allocate की जाती है |

अगर User ने integer data type का variable बनाया तो 16-bit पर 2 bytes या 32-bit पर 4 bytes की memory allocate की जाती है |


Memory Allocation के लिए दो प्रकार है |

  1. Compile-time/Static Memory Allocation
  2. Run-time/Dynamic Memory Allocation

1. Compile-time/Static Memory Allocation

  • जब variable को बनाया जाता है, तब Compiler के जरिये compile-time पर memory allocate की जाती है |
  • जब variable बनता है , तब वो किस data type से बनता है, तो compiler इसके हिसाब से खुद ही वो memory allocate करता है |
  • Variable का declaration Compile-time पर होता है |
  • For Example,
    32-bit Integer के लिए 4 bytes
    Character के लिए 1 byte
    float के लिए 4 bytes
  • जब array को Compile-time पर memory allocate करे तो ये memory का space ये constant होता है | For Example,
    int arr[10];
    जब ऐसा array बनता है, तो इसमें 10 ही integer की values declare की जा सकती है और हर variable 4 bytes की memory allocate करेगा |

 

2. Run-time/Dynamic Memory Allocation

  • Dynamic Memory Allocation में Run-time पर Memory allocate की जाती है |
  • Dynamic Memory Allocation में जब program run होता है तब Memory allocate की जाती है |
  • यहाँ पर जरुरत के हिसाब से variables को बनाया जाता है |
  • Dynamic Memory Allocation में variables का declaration नहीं किया जाता | सिर्फ compile-time पे variable को declaration किया जाता है |

C Programming में जैसे Dynamic Allocation के लिए malloc, calloc, realloc और free का इस्तेमाल किया जाता है , वैसे ही C++ में दो ऐसे Operators है , जिनका इस्तेमाल Dynamic Memory Allocation के लिए किया जाता है |

  1. new operator
  2. delete operator

1. new operator

new operator का इस्तेमाल Dynamic memory को allocate करने के लिए किया जाता है |

ये memory allocation; Run-time पर होता है

Syntax for new

यहाँ पर new operator के साथ कौनसे भी data type की और array के साथ dynamic memory allocate की जा सकती है | यहाँ पर memory allocation के class को भी लिया जा सकता है |

new data_type
new data_type[array_size];
For Example.
new int;
new int[10];

जब dynamic memory allocation करना हो तो 'pointer' का इस्तेमाल किया जाता है | जब new operator एक dynamic allocation है तब उसका address; new operator से दिए हुए pointer में store किया जाता है |

Syntax for new operator with pointer

pointer = new data_type;
pointer = new data_type[array_size]; //for Array

Dynamic Memory Allocation के Example में देखा जाए, तो वहा new के साथ int का address; store करने के लिए int *ptr लिया है और ptr में new से int का address return होकर ptr के अन्दर store होगा |

Pointer में जैसे integer data type variable का address hold करना है तो pointer भी उसी data type का होता है वैसे ही new int address store करना हो तो pointer भी nteger का ही होना चाहिए |

For Example
int *ptr;
ptr = new int;
ptr = new int[50];  //for array
or
int *ptr = new int;
int *ptr = new int[50];  //for array

किसी User से भी array का size define किया जाता है |

For Example
int a;
cin>>a;
int *ptr;
ptr = new int[a]; or int *ptr = new int[a];

delete Operator

delete operator ये allocated dynamic memory को de-allocate करता है |

जब new operator; memory allocate करता है, तब delete operator से allocate की हुई memory को de-allocate या free कर देता है |

जब program में new operator इस्तेमाल किया जाता है, तब delete operator का इस्तेमाल किया जाता है |

Syntax for delete

delete pointer_name;
delete []pointer_name; //for array

Example for delete

delete ptr;
delete []ptr; // for array

Example for new and delete operator

Source Code :
#include <iostream.h>
using namespace std;

int main(){

int *ptr;
ptr = new int;

cout<<"Enter value of ptr : ";
cin>>*ptr;
cout << "Value is ptr : " <<*ptr<< endl;

delete ptr;

return 0;
}
Output :
Enter value of ptr : 2
Value is ptr : 2

Example for new and delete operator with array

Source Code :
#include <iostream.h>
using namespace std;

int main(){

int a, i;
int *ptr;

    cout<<"Enter number of elements : ";
    cin>>a;

ptr = new int[a];

    cout<<"Enter "<<a<<" elements "<<endl;

for(i=1; i<=a; i++){

    cout<<"Element "<<i<<" : ";
    cin>>ptr[i];
}
cout<<"Entered Elements :"<<endl;
for(i=1; i<=a; i++){

    cout<<ptr[i]<<endl;
}

delete []ptr;

return 0;
}
Output :
Enter number of elements : 4
Enter 4 elements
Element 1 : 6
Element 2 : 4
Element 3 : 8
Element 4 : 9
Entered Elements :
6
4
8
9

class with Constructor, Destructor and array

Source Code :
#include <iostream.h>
using namespace std;

class A{
public:
    A(){
    cout << "Constructor"<<endl;
}
    ~A(){
    cout << "Destructor"<<endl;
}
};

int main()
{
    A *a = new A[3];
    delete[] a;

return 0;
}
Output :
Constructor
Constructor
Constructor
Destructor
Destructor
Destructor