Memory allocation in C++ in Hindi
C++, में Memory allocation, Program execution के दौरान Computer की Memory portion को Reserve और Manage करने के Process को refer करता है।यह Program execution के दौरान Variables या Data structure को Memory portion Assign करने की प्रक्रिया है।
Error को रोकने और Program की Stability को सुनिश्चित करने के लिए Memory का सही Management महत्त्वपूर्ण है।
Types of Memory allocation in C++ in Hindi
2) Dynamic Memory Allocation
Static Memory Allocation in C++ in Hindi
C++ में, Static memory allocation, Memory को manage करने की एक तकनीक है।जब Memory को Program execution के पहले Compile time में Allocate किया जाता है तो वह Static memory allocation कहलाता है।
Memory को Variables और Data structures को Declaration के समय Allocate किया जाता है, और पूरे Program execution के दौरान वह Allocate रहता है।
Static allocation को `static` keyword, का Use करके किया जाता है , जो Variables और Objects के लिए Memory Reserve करता है।
Memory को Allocate करने के कार्य को एक बार ही किया जाता है और उसी Memory location को हर समय Use किया जाता है।
उनका आकार निश्चित है और उसे Compile-time में निर्धारित किया जाता है तथा पूरे Program के Execution के दौरान उनकी Value बरकरार रहती है।
Static allocation को आमतौर पर Global variables, static variables और functions के अंदर तथा const` keyword के साथ Constants को Declare करते समय Use किया जाता है।
Static memory को Manage करना सरल है क्योंकि इसमें Runtime के दौरान, Dynamic Memory Allocation की तरह Allocation or Deallocation शामिल नहीं है।
#include<iostream>
using namespace std;
// Static memory allocation with a static variable and function
void displayStatic()
{
static int count = 10;
count++;
cout << "Static Variable: " << count<<endl;
}
int main() {
cout << "Calling displayStatic()1st time:" << endl;
displayStatic();
cout << "Calling displayStatic() 2nd time:" <<endl;
displayStatic();
return 0;
}
Calling displayStatic() 1st time:
Static Variable: 11
Calling displayStatic() 2nd time:
Static Variable: 12
1) Function को Define करना:
- `void displayStatic()` नाम से एक static variable के साथ एक function define किया गया है।
2) Static variable:
- `static int count = 10;`
एक static variable `count` को 10 Value assign कर Initialize किया गया है।
3) Static variable को Increment करना:
`count++;`
`count` को Increment किया गया है।
4) Static variable की Value को Print करना:
- `cout << "Static Variable: " << count << endl;`
से `count` की Value Print की जाती है।
4) Main Function:
`displayStatic()` को First time call करने से 11 Prints करता है।
तथा इसे Second time Call करने पर 12 Print करता है।
अतः Static variable `count` function calls के बीच में अपनी Value को बनाए रखता है।
Dynamic memory allocation in C++ in Hindi
C++ में Runtime के दौरान Special operators जैसे `new` और `delete` का Use करके जब Memory को allocate किया जाता है तब यह Dynamically memory allocation कहलाता है।C++ जैसी प्रोग्रामिंग भाषाएं, Dynamic memory allocation करने की अनुमति देता है जिसका आकार Static allocation की तरह Compile time के बजाय Runtime के दौरान निर्धारित होता है।
इसका मतलब यह है कि आवश्यक Memory की सटीक मात्रा की गणना Users Input, Program situations या अन्य Runtime factors के आधार पर की जा सकती है।
Static allocation के विपरीत Dynamic allocation, Variable-sized के memory के Request की अनुमति देता है और इसे प्रोग्रामर द्वारा Manual रूप से Manage किया जाता है।
Flexibility की आवश्यकता
Management Operators in C++ in Hindi
1) New Operator2) Delete Operator
New Operator
C++ में `new` operator का Use, Runtime के दौरान Dynamic memory allocation के लिए किया जाता है।यह Arrays, Objects और अन्य Data structures के लिए, Memory को Heap (Free Store) पर Dynamically allocates करता है और उस Allocated memory के Memory address को Store करते हुए एक Pointer return करता है।
new operator, Operating system से Memory प्राप्त करता है और उसके Starting point को निर्दिष्ट करने वाला Pointer return करता है।
यह आपको Stack के बजाय Heap पर Objects का निर्माण करने की अनुमति देता है जो आपको विभिन्न आकारों और प्रकारों में Objects का निर्माण करने में सक्षम बनाता है और Stack पर Static allocation की तूलना मे Memory को Manage करना और अधिक लचीला बनाता है।
Syntax:
data-type *pointer-name = new data -type;
ऊपर Syntax में data-type int, float char, array, class इत्यादि हो सकते है।
हमने data-type का उपयोग दो बार किया है, पहली बार पहली बार request किए गए Memory के Type का उल्लेख करने के लिए और दूसरी बार एक उपयुक्त Pointer को Declare करने के लिए।
Example:
int *ptr = new int;
ऊपर के Example में new int, Program को यह बताता है कि हमें एक int Data को रखने के लिए कुछ नई व उपयुक्त Storage चाहिए new operator इस type(int)का उपयोग यह पता लगाने के लिए करता है कि कितने Bits की जरूरत है।
तब यह ऑपरेटिंग सिस्टम से मेमोरी को खोजना है और Address return करता है। यहां हम ptr को Address assign करते है जो int type का Pointer declare किया गया है।
इसलिए ptr एक Address है और *ptr वहां Store की गई value है।
Delete Operator
C++ में, Delete operator का Use, new operator द्वारा Dynamically allocate किए गए Memory space को Deallocate(हटाने) के लिए किया जाता है।Memory की सुरक्षित और कार्य कुशल उपयोग को सुनिश्चित करने के
यह Memory leak को रोकते हुए Memory को System में वापस भेजता है।
Delete operator का उपयोग एक Object के लिए किया जाता है, जबकि delete[] का उपयोग Arrays के लिए किया जाता है।
उदाहरण के लिए, delete ptr; एक Object के लिए Memory को Deallocate(हटा) देता है, जबकि delete[] arr; किसी Array के लिए Memory को Deallocate देता है।
new के बाद यदि हम delete operator का उपयोग नहीं करते तो Memory leak हो सकती है, जिससे Memory का उपयोग अप्रभावी हो सकता है।
delete operator का उचित उपयोग यह सुनिश्चित करता है कि Dynamically allocated memory को ठीक से Manage किया जाता है और आवश्यकता न होने पर मुक्त किया जाता है।
Syntax:
delete pointer;
Example:
float *fptr = new float;
delete *fptr;
#include<iostream>
using namespace std;
int main(){
int *ptr = new int;
*ptr= 5;
cout<<"Address: "<<ptr<<endl;
cout<<"Value: "<<*ptr<<endl;
delete ptr;
return 0;
}
Address: 0x55bcf261deb0
Value: 5
int *ptr = new int;
ऊपर का Statement एक Integer के लिए Dynamically memory को allocate करता है।
2) Value assign करना:
*ptr = 5;
Allocate किए गए Memory को 5 Value assign किया जाता है।
3)Address और Value को Print करना:
cout << "Address: " << ptr << endl;
cout << "Value: " << *ptr << endl;
ऊपर के Statements के द्वारा Memory address तथा उस Address में Store value को Print की जाती है।
4) Memory को Deallocate(free) करना:
delete ptr;
यह statement frees the allocated memory को खाली कर देता है।
#include<iostream>
using namespace std;
int main() {
int *ptr = new int[5];
for(int i = 0; i < 5; ++i) {
*(ptr + i) = i * 2;
}
for(int i = 0; i < 5; ++i) {
cout << "Value " << i + 1 << ": " << *(ptr + i) << endl;
}
delete[] ptr;
return 0;
}
Value 1: 0
Value 2: 2
Value 3: 4
Value 4: 6
Value 5: 8
Explaination:
1) Memory को allocate करना:
int *ptr = new int[5];
2) Array को Initialize करना:
for(int i = 0; i < 5; ++i) { *(ptr + i) = i * 2; }:
यह Statement Array को 0, 2, 4, 6, और 8 Values देता है
for(int i = 0; i < 5; ++i) { cout << "Value " << i + 1 << ": " << *(ptr + i) << endl; }:
यह Statement , Array में प्रत्येक Value को Print करता है।
4) Memory को Deallocate(Free) करना:
delete[] ptr;
Array के लिए Alllocated memory को खाली किया जाता है।
Related Posts
> C++ क्या है? उसके इतिहास, गुण, उपयोग, फायदे और नुकसान
> Basic structure of C++ Program
> C++ में Tokens क्या है? और उसके प्रकार
> C++ Variables क्या है?, उसके प्रकार, उसे कैसे Declare, Define करते हैं
> C++ में Constants क्या है? उसके प्रकारों की संपूर्ण जानकारी
> C++ में Basic Input और Output (cin,cout,cerr) की जानकारी
> Data type in C++ की संपूर्ण जानकारी
> C+ में Operators और उसके प्रकार जानें Practical सहित
> C++ में Conditional और उसके प्रकारों को जानें Practical सहित
> C++ में Looping statements और उसके प्रकार Practical सहित
> C++ में Jump Statements और उसके प्रकारों की संपूर्ण जानकारी Practical सहित
> C++ में Array क्या है? और उसके प्रकारों की जानकारी Practical सहित
> C++ में Function क्या है उसके प्रकार, उपयोग प्रोग्राम सहित
> C++ में Structure क्या है Practical सहित
> OOPs Concepts in C++ in Hindi- C++ में OOPs के बारे में
> Oops के फायदे और नुकसान की जानकारी
> C++ में Class और Object की सम्पूर्ण जनकारी
> C++ में Array of Objects क्या है?
> C++ में Pointers, Pointer to an objects, Pointer to an Array की संपूर्ण जानकारी हिंदी में।
> C++ में Passing objects क्या है
> C++ में Reference और Type Casting की संपूर्ण जानकारी
> C++ में Access specifier की संपूर्ण जानकारी
> C++ में Static Data Members और Member Functions के बारे में Practical सहित
> Friend Function in C++ in Hindi
> Friend Class in C++ in Hindi Practical सहित
> Inline function in C++ in Hindi
> Function Overloading in C++ in Hindi Practical सहित
> Operator Overloading in C++ in Hindi Practical सहित
> C++ में Constructor क्या है और उसके प्रकारों की संपूर्ण जानकारी
> C++ में Destructor क्या है ?उसकी संपूर्ण जानकारी
> C++ मे Inheritance क्या है उसके प्रकारों को जानें प्रोग्राम सहित
> C++ में Polymorphism क्या है? और उसके प्रकारों को जानें
> C++ में Virtual function की संपूर्ण जानकारी
> C++ में File handling की संपूर्ण जानकारी
> C++ में Exception handling की संपूर्ण जानकारी
0 टिप्पणियाँ