C में Dynamic memory allocation की संपूर्ण जानकारी

What is Dynamic memory allocation in C in Hindi


C में Dynamic memory allocation Compile Time की अपेक्षा Runtime में अर्थात Program execution के दौरान होने की प्रक्रिया को refer करता है

सरल शब्दों कहे तो Memory को allocate करने का कार्य Compile Time में न होकर Program के execution के दौरान हो, तो यह Dynamic memory allocation कहलाता है।
C में Memory allocation, Program execution के दौरन variables और data structure को memory space देने की प्रक्रिया को कहते है इससे programs बड़े ही flexible ढंग से memory को manage कर पाता है 

Types of Memory allocation in C in Hindi

C में Memory allocation को दो तरीके से किया जाता है।
 
1) Static Memory Allocation:
2) Dynamic Memory Allocation:

Static Memory Allocation


Static memory allocation compile time में होता है और ऐसे variables और data structures के लिए memory allocate की जाती है जिसका size पहले से पता हो  

इसके अंतर्गत global variables, static variables और एक fixed size से declare किया गया array आदि आते है
static allocation में variables को program के memory में Data segment के रूप में allocate की जाती है

Static memory allocation का अर्थ memory को compile time में allocate किया जाना है और program execution के दौरान इनका स्थिर बने रहना है।
Program for Static memory allocation
#include<stdio.h>
int main() 
{
// Here variable's Static memory allocation is showing
    int x = 32;
    float y = 4.56;

 printf("Value of x:%d\n",x);
 printf("Value of y:%f\n",y);
   
    return 0;
}
 

Output:
Value of x: 32
Value of y: 4.560000

Explanation:
1) ऊपर C program variables `x` और `y` declare किया गया है इसमें 'x' variable int data type के कारण 2 bytes और 'y' variable float data type के कारण 4 bytes memory लेता है

2) दोनो variables को Compile time में static रूप से memory allocate किया गया है
 
3) `x` और `y` की memory पूरे program execution के दौरान fixed रहेगी

Dynamic Memory Allocation

Dynamic memory allocation के अंतर्गत memory allocation का कार्य runtime में या program execution के दौरान function जैसे occurs `malloc`, `calloc`, और `realloc` का Use करके किया जाता है

यह को program को runtime condition के आधार पर या User के Input के आधार पर आवश्यकतानुसार memory allocate करने की अनुमति देते है

C में Memory को Dynamically allocate करने के लिए निम्न 4 Functions का Use किया जाता है
1) malloc( )
2) calloc( )
3) realloc( )
4) free( )

malloc( )

C में `malloc` function का Use dynamic memory allocation के लिए किया जाता है malloc का पूरा नाम "memory allocation." है 

इस function की सहायता से Program के शुरुआत मे fixed amount में memory allocate करने के बजाय हम Program को run होने के दौरान memory allocate कर सकते है 

इस method में Memory के एक बड़े block (heap memory) को specified size (bytes में) के साथ program execution के दौरान allocate किया जाता है

यह महत्त्वपूर्ण है कि allocated memory का उपयोग हो जाने के बाद हमे free function का Use करके उस allocated memory को खाली करना होता है ताकि वह memory space अन्य चीजों के लिए Use किया जा सके

Syntax:
ptr = (castType*) malloc (size_type);

Example:
ptr = (int*) malloc (5*sizeof(int));

ऊपर के Example में 10 bytes memory space allocate किया जायेगा क्योंकि एक integer memory में 2 bytes स्थान लेता है अत: 
malloc( 5*2) = 10 bytes होगा
Program for malloc function
#include<stdio.h> 
#include<stdlib.h> 
int main() 
{
    int* ptr;

// Here we allocate memory for a single integer
    ptr = (int*)malloc(sizeof(int));

if (ptr == NULL) 
     {
        printf("Memory allocation failed\n");
        exit(0);
     }
//Assigning a value to allocated memory
    *ptr = 25;
    printf("Value of ptr = %d", *ptr);
   
    free(ptr);
    return 0;
}
 
Output:
Value of ptr = 25

Explanation:
1) सबसे पहले हमने एक integer pointer ptr को declare किया है और malloc(sizeof(int)) का Use करके एक single integer के लिए memory allocate किए है

2) sizeof(int) भाग यह सुनिश्चित करता है कि एक Integer data type के लिए पर्याप्त memory allocate किया गया है 

3) if condition लगाकर यह check करते है कि Memory allocation failed तो नही हुआ यदि नही हुआ तो pointer ptr का Use करके हम allocated memory को 42 value assign करते है

4) अब allocated memory में store value को print करते है और अंत मे free (ptr) function का Use करके allocated memory को खाली कर देते है

calloc( )

calloc का पूरा नाम "contiguous allocation" है. C में calloc function का Use भी malloc function के समान memory को dynamically allocate करने में किया जाता है

लेकिन दोनो मे कुछ अंतर भी है जैसे malloc() से अलग calloc() का Use memory के कई block को allocate करने में किया जाता है जैसे array के elements को store करने के लिए

साथ ही calloc() का Use करते समय memory के प्रत्येक block का initialization by default 0 के साथ होता है calloc() में malloc() की अपेक्षा दो parameters या arguments होते है

Syntax:
ptr = (castType*) calloc (size_type);

Example:
ptr = (float*) calloc (10*sizeof(float));

ऊपर के example में 10 elements के लिए total 50 bytes contiguous memory space allocate होगा जिसमे प्रत्येक element हेतु 4 bytes memory space allocate होगा क्योंकि float, memory में 4 bytes लेता है 
Program for calloc function
#include<stdio.h> 
#include<stdlib.h> 
int main() 
{
    int* ptr;
// Allocate memory for an array of 5 integers  
   
 ptr = (int*)calloc(5, sizeof(int));

  if(ptr==NULL)
   {
     printf("Memory allocation failed\n"); 
        exit(0);
   }
    
  for (int i = 0; i < 5; i++) 
       {
        ptr[i] = i+1;
       }

  printf("Array elements are: ");
 
  for (int i = 0; i < 5; i++) 
    {
        printf("%d", ptr[i]);
     }
 
// Free the allocated memory
    free(ptr);
    return 0;
}
 
Output:
Array elements are: 12345

Explaination:
1) ऊपर के Program में हमने  `calloc` function का Use करके 5 integers वाले एक array को memory allocate गया किया है 

2) memory allocate करने के बाद array elements को 1 से 5 तक values assign किया गया है और उसके बाद उन values को print किया जाता है तथा अंत में allocated memory को free कर देते है

realloc( )

realloc का पूरा नाम "re-allocation" है इस function का Use, malloc, calloc, या realloc function का Use करके पहले से dynamically allocate किए गए memory block को reallocate करने या एक block की size को बढ़ाने या घटाने के लिए किया जाता है

दूसरे शब्दों में कहे तो malloc या calloc function की मदद से जो memory block हमने dynamically allocate किया है यदि वह memory block अपर्याप्त है तब realloc function का use memory को dynamically re-allocate करने मे होता है।
 
Syntax:
ptr = realloc (ptr, new_size);

Example:
ptr = (float*)realloc(ptr, 10 * sizeof(float));

ऊपर के Example में 'float' Data type के 10 elements को स्थान देने के लिए पहले से allocated pointer 'ptr' के लिए memory को reallocate किया जाता है, जिससे उचित memory को सही तरीके से Manage किया जा सके और Data structure का संभावित आकार सुनिश्चित किया जा सके

Program for realloc function
#include<stdio.h> 
#include<stdlib.h> 

int main()
  {
    int* ptr;

// Allocate memory for an array of 5 integers
    ptr = (int*)malloc(5 * sizeof(int));

    if (ptr == NULL) 
      {
        printf("Memory allocation failed\n");
        exit(0);
      }

// Assigning values to the allocated memolry 
    for (int i = 0; i < 5; i++) 
      {
        ptr[i] = i + 1; // Assign values 1, 2, 3, 4, 5
      }
   
    printf("Before reallocation array'elements: ");
    
    for (int i = 0; i < 5; i++)
      {
        printf("%d", ptr[i]);
      }
    printf("\n");

// Reallocate memory for an array of 7 integers
    
    ptr = (int*)realloc(ptr, 7 * sizeof(int));

    if (ptr == NULL) 
     {
    printf("Memory reallocation failed\n");
        exit(0);
     }

// Assigning values to the reallocated memory 
    
    for (int i = 5; i < 7; i++) 
      {
        ptr[i] = i + 1; 
      }

    printf("After reallocation array'elements: ");
    
    for (int i = 0; i < 7; i++) 
     {
        printf("%d", ptr[i]);
     }
     printf("\n");

// Free the allocated memory
    free(ptr);
    return 0;
}
 
Output:
Before reallocation array'elements: 12345
After reallocation array'elements: 1234567

Explanations:
1) सबसे पहले हमने malloc function कर Use करके 5 Interger elements वाले array के हेतु memory allocate किए

2) उसके बाद पहले 5 integer elements को वैल्यूज assign करने के बाद हमने realloc function का Use करके 7 integers elements के एक array को memory reallocate किए

3) पहले 5 integer को दिया गया value ज्यों का त्यों बना रहा और हमने पुन: 2 integers value और reallocate किए

4) उसके बाद अंत मे realloc ation के पहले और बाद में array elements के value print किए
और allocated memory को free function का Use करके खाली कर दिया

free( )

malloc() या calloc() का Use करके जो Memory block पहले allocate किया गया था उस block को free( ) function का use करके program execution के दौरान deallocate (free) किया जाता है ताकि उस memory space का आगे फिर से use किया जा सकें

अतः जब भी dynamicall memory allocation का Use करते है free() function का हमेशा Use होता है क्योंकि यह memory को नुकसान होने से बचाता है।

Syntax:
free(ptr);



 Related Post