Notes in Hindi

Static and Dynamic Memory Allocation in Hindi

/ BCA / Programming with C and CPP

Static and Dynamic Memory Allocation in Hindi

Static and Dynamic Memory Allocation in Hindi

What is Static and Dynamic Memory Allocation in Hindi

जब हम C या C++ जैसी programming language में कोई program बनाते हैं, तो उसमें हमें variables और data structures को memory में store करने की ज़रूरत होती है। ये memory दो तरीकों से allocate की जा सकती है: Static Memory Allocation और Dynamic Memory Allocation।

  • Static Memory Allocation: इस प्रकार की memory allocation compile time पर होती है। इसका मतलब है कि जब program compile होता है, तभी memory का size fix कर दिया जाता है।
  • Dynamic Memory Allocation: इस प्रकार की memory allocation run time पर होती है। इसका मतलब है कि program चलने के दौरान memory allocate की जाती है, जब जरूरत पड़ती है।

Static Allocation में memory एक बार allocate हो जाती है और पूरी program के execution के दौरान वही रहती है, जबकि Dynamic Allocation में हम program के execution के दौरान जरूरत के अनुसार memory allocate और deallocate कर सकते हैं।

Difference Between Static and Dynamic Allocation in Hindi

Static Memory Allocation Dynamic Memory Allocation
Memory compile time पर allocate होती है। Memory run time पर allocate होती है।
Memory का size पहले से fix होता है। Memory का size execution के समय decide किया जा सकता है।
Memory को manually free करने की जरूरत नहीं होती। Memory को manually free करना पड़ता है।
Memory stack पर allocate होती है। Memory heap पर allocate होती है।
कम flexibility होती है। ज्यादा flexibility होती है।
उदाहरण: int a[10]; उदाहरण: int *a = (int*)malloc(10 * sizeof(int));

Advantages of Dynamic Memory Allocation in Hindi

  • Flexible Memory Management: जब हमें पहले से नहीं पता होता कि कितनी memory की जरूरत होगी, तब हम Dynamic Allocation का उपयोग करके जितनी जरूरत हो उतनी memory ले सकते हैं।
  • Efficient Resource Utilization: Memory की बर्बादी नहीं होती, क्योंकि हम उतनी ही memory allocate करते हैं जितनी हमें जरूरत होती है।
  • Scalable Programs: Program को future में modify करना आसान होता है, जैसे कि अगर user से input लेना हो और उसके हिसाब से memory लेनी हो।
  • Data Structures में उपयोग: Linked List, Tree, Graph आदि जैसे advanced data structures dynamic memory पर आधारित होते हैं।
  • Memory को control करने की सुविधा: Programmer यह decide कर सकता है कि कब memory लेनी है और कब उसे free करना है।

Common Functions Used in Memory Allocation in Hindi

जब हम dynamic memory allocation करते हैं, तब C language में कुछ खास functions का उपयोग करते हैं जो memory को allocate और free करने में मदद करते हैं। ये functions <stdlib.h> header file में होते हैं।

  • malloc(): यह function memory allocate करता है लेकिन उसे initialize नहीं करता। यह एक pointer return करता है जो उस memory block की शुरुआत को point करता है।
  • calloc(): यह function भी memory allocate करता है लेकिन यह allocated memory को 0 से initialize करता है।
  • realloc(): यह पहले से allocated memory block को नया size देने के लिए use होता है।
  • free(): यह function previously allocated memory को release करने के लिए use होता है, ताकि memory leak न हो।

Examples of Memory Allocation Functions in C

malloc() Example:

#include <stdio.h>
#include <stdlib.h>

int main() {
  int *ptr;
  ptr = (int*) malloc(5 * sizeof(int));

  if(ptr == NULL) {
    printf("Memory not allocated!");
    return 1;
  }

  for(int i = 0; i < 5; i++) {
    ptr[i] = i + 1;
  }

  for(int i = 0; i < 5; i++) {
    printf("%d ", ptr[i]);
  }

  free(ptr);
  return 0;
}

calloc() Example:

#include <stdio.h>
#include <stdlib.h>

int main() {
  int *ptr;
  ptr = (int*) calloc(5, sizeof(int));

  if(ptr == NULL) {
    printf("Memory not allocated!");
    return 1;
  }

  for(int i = 0; i < 5; i++) {
    printf("%d ", ptr[i]);
  }

  free(ptr);
  return 0;
}

realloc() Example:

#include <stdio.h>
#include <stdlib.h>

int main() {
  int *ptr;
  ptr = (int*) malloc(2 * sizeof(int));

  ptr[0] = 10;
  ptr[1] = 20;

  ptr = (int*) realloc(ptr, 4 * sizeof(int));
  ptr[2] = 30;
  ptr[3] = 40;

  for(int i = 0; i < 4; i++) {
    printf("%d ", ptr[i]);
  }

  free(ptr);
  return 0;
}

free() Example:

#include <stdlib.h>

int main() {
  int *ptr = (int*) malloc(100 * sizeof(int));
  // Memory use करें
  free(ptr); // Memory free करें
  return 0;
}

जब भी dynamic memory allocation का उपयोग करें, यह सुनिश्चित करें कि आप free() का उपयोग करके memory को release कर रहे हैं, नहीं तो memory leak हो सकता है जिससे program की performance पर असर पड़ेगा।

Important Tips for Students

  • Static memory allocation आसान है लेकिन flexible नहीं होता।
  • Dynamic memory allocation flexible होता है लेकिन थोड़ा ध्यान से use करना पड़ता है।
  • Memory को free करना न भूलें, वरना memory leak हो सकता है।
  • Always check malloc, calloc, realloc का return value NULL तो नहीं है, इससे segmentation fault से बच सकते हैं।

इन concepts को समझने से आप real-world में efficient programs बना सकते हैं, जैसे कि data structures, file handling, operating system design आदि में।

FAQs

Static Memory Allocation वह प्रक्रिया है जिसमें memory compile time पर allocate होती है। इसमें memory का size पहले से fix होता है और program के execution के दौरान नहीं बदला जा सकता। जैसे: int a[10]; में 10 integers की memory पहले से allocate हो जाती है।
Dynamic Memory Allocation वह प्रक्रिया है जिसमें memory run time पर allocate होती है। इसका मतलब है कि program के execution के दौरान जरूरत के हिसाब से memory को allocate या free किया जा सकता है। इसके लिए malloc(), calloc(), realloc() जैसे functions का उपयोग किया जाता है।
Static Allocation में memory compile time पर fix होती है जबकि Dynamic Allocation में memory run time पर allocate होती है। Static Allocation में flexibility कम होती है, लेकिन यह आसान होता है। Dynamic Allocation flexible होता है और complex programs के लिए उपयोगी होता है।
Dynamic Memory Allocation का उपयोग तब किया जाता है जब हमें पहले से यह नहीं पता होता कि कितनी memory की जरूरत होगी। इससे memory का बेहतर उपयोग होता है और हम runtime पर data structures जैसे Linked List, Trees आदि को manage कर सकते हैं।
Dynamic Memory Allocation के लिए मुख्यतः चार functions का उपयोग होता है:
  • malloc() – memory allocate करता है
  • calloc() – memory allocate करता है और initialize करता है
  • realloc() – पहले से allocated memory का size बदलता है
  • free() – allocated memory को release करता है

Please Give Us Feedback