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 के लिए दो प्रकार है |
- Compile-time/Static Memory Allocation
- 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,
- जब array को Compile-time पर memory allocate करे तो ये memory का space ये constant होता है |
For Example,
int arr[10];
जब ऐसा array बनता है, तो इसमें 10 ही integer की values declare की जा सकती है और हर variable 4 bytes की memory allocate करेगा |
32-bit Integer के लिए 4 bytes
Character के लिए 1 byte
float के लिए 4 bytes
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 के लिए किया जाता है |
- new operator
- 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 Exampleint *ptr; ptr = new int; ptr = new int[50]; //for arrayor
int *ptr = new int; int *ptr = new int[50]; //for array
किसी User से भी array का size define किया जाता है |
For Exampleint 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 :Output :#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; }
Enter value of ptr : 2 Value is ptr : 2
Example for new and delete operator with array
Source Code :Output :#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; }
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 :Output :#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; }
Constructor Constructor Constructor Destructor Destructor Destructor