Dynamic Memory Allocation in C – Hindi में Example के साथ

Hello दोस्तों! क्या आपने कभी सोचा है कि जब हम C programming में
int arr[10];
लिखते हैं, तो हम computer को पहले ही बता देते हैं कि हमें सिर्फ 10 integer values store करने के लिए memory reserve कर दो।

लेकिन अगर program के runtime पर हमें 10 की जगह 50 values store करनी पड़ जाएँ, या फिर सिर्फ 2 values की ज़रूरत हो, तब क्या करेंगे?

ऐसी situations में Dynamic Memory Allocation in C काम आता है। इस ब्लॉग में हम इस concept को इतने आसान तरीके से समझेंगे कि आपको यह हमेशा याद रहेगा। चलिए शुरू करते हैं!

Dynamic Memory Allocation क्या है?

आसान शब्दों में कहें तो, program के चलने के दौरान (runtime) जरूरत के हिसाब से जो memory allocate की जाती है, उसे dynamic memory allocation कहते हैं।

Real-life programming example:
मान लीजिए हमें students का data store करना है, लेकिन हमें पहले से नहीं पता कि कितने Student का Data Store करना है। ऐसे में dynamic memory allocation best solution है।

Dynamic memory allocation सीधे Heap memory से जुड़ा होता है, इसलिए पहले Heap और Stack को समझना ज़रूरी है। तो चलिये Heap और Stack Memory के Concept को समझते हैं :

Heap और Stack Memory क्या होती है?

C Language में memory को mainly दो हिस्सों में बाँटा गया है:

1. Stack Memory

Stack memory वह जगह होती है जहाँ local variables और function calls store होते हैं। यह memory automatic तरीके से manage होती है और इसका size पहले से fixed होता है। जैसे ही function का execution खत्म होता है, Stack Memory अपने आप free हो जाती है।

Stack memory की सबसे बड़ी limitation यह है कि हम runtime पर memory का size कम या ज़्यादा नहीं कर सकते। Stack memory को program खुद handle करता है, programmer को इसमें manually कुछ नहीं करना पड़ता।

Example (Stack Memory):

int num() {
    int x;
	printf(“Enter a Number : “);
	scanf(%d”,&x);
	Return x;
}

यहाँ x stack memory में store  होगा। और function खत्म होते ही automatically delete भी हो जायेगा।

2. Heap Memory

Heap memory system memory का वह हिस्सा है, जो dynamic memory allocation के लिए use होता है। C language में dynamic memory runtime पर allocate की जाती है, इसलिए Heap memory का size flexible होता है।

लेकिन Heap memory में programmer की responsibility होती है कि वह memory को खुद allocate करे और काम पूरा होने के बाद उसे free भी करे। Heap memory में runtime के दौरान memory का size कम या ज़्यादा किया जा सकता है।

Dynamic Memory Allocation in C

Dynamic Memory Allocation के Functions in C (stdlib.h)

C language में Dynamic Memory को Allocate करने के लिए कुछ खास Function का इस्तेमाल किया जाता है जो निचे सब Function को Explain किया गया।  इन सब Function को इस्तेमाल करने के  लिए Program में Stdlib.h नाम के Header File को Include करते है। 

1. malloc() क्या है?

malloc() run-time पर memory allocate करता है और allocated memory में initial value नहीं होती, इसलिए by Default garbage values होती हैं।

Syntax:

ptr = (cast-type*) malloc(byte-size);

Example:

int *ptr;

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

Line-by-line Explanation:

  • ptr: यह उस Memory का पता (address) Store करेगा जिसे हम Allocate करेंगे।
  • (int*): इसे ‘Type Casting’ कहते हैं। malloc खाली जगह देता है, हम उसे बता रहे हैं कि हमें ‘integer’ Value को Store करने के लिये जगह चाहिए।
  • 5 * sizeof(int): इस Line का मतलब है, कि 5 integer जितनी जगह दे दो (आमतौर पर 5 x 4 = 20 bytes)।

Example Program Using Malloc Function in C

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

int main()
{
    int size, i;
    int *ptr;

    /* Input size */
    printf("Enter how many numbers you want to store: ");
    scanf("%d", &size);

    /* Memory allocation */
    ptr = (int *)malloc(size * sizeof(int));

    /* Check allocation */
    if (ptr == NULL)
    {
        printf("Memory not allocated.\n");
        return 0;
    }

    /* Input values */
    printf("Enter %d values:\n", size);
    for (i = 0; i < size; i++)
    {
        scanf("%d", &ptr[i]);
    }

    /* Display values */
    printf("Stored values:\n");
    for (i = 0; i < size; i++)
    {
        printf("Value %d : %d\n", i + 1, ptr[i]);
    }

    /* Free memory */
    free(ptr);
    ptr = NULL;

    return 0;
}

Output:

Enter how many numbers you want to store: 4

Enter 4 values:

78

90

55

32

Stored values:

Value 1 : 78

Value 2 : 90

Value 3 : 55

Value 4 : 32

2. calloc() क्या है?

calloc() का मतलब है Contiguous Allocation। यह malloc() की तरह run-time पर memory allocate करता है, लेकिन फर्क यह है कि calloc() से मिली पूरी memory 0 (zero) से initialize होती है जबकि malloc() Function Allocate की गयी Memory में By Default Garbage Value होती है।

Example:

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

इस लाइन का मतलब है कि हमने Computer से 5 integers के लिए जगह मांगी है। calloc की खासियत यह है कि यह उस जगह को खाली छोड़ने के बजाय, खुद ही हर block में शुरुआत में 0 Initialize होता है, ताकि कोई पुराना कचरा (Garbage Value) न रहे।

Example Program Using Calloc Function in C

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

int main()
{
    int size, i;
    int *ptr;

    /* Input size */
    printf("Enter how many numbers you want to store: ");
    scanf("%d", &size);

    /* Memory allocation using calloc */
    ptr = (int *)calloc(size, sizeof(int));

    /* Check allocation */
    if (ptr == NULL)
    {
        printf("Memory not allocated.\n");
        return 0;
    }

    /* Display default values */
    printf("Default values in allocated memory:\n");
    for (i = 0; i < size; i++)
    {
        printf("Value %d : %d\n", i + 1, ptr[i]);
    }

    /* Input values */
    printf("Enter %d values:\n", size);
    for (i = 0; i < size; i++)
    {
        scanf("%d", &ptr[i]);
    }

    /* Display updated values */
    printf("Stored values:\n");
    for (i = 0; i < size; i++)
    {
        printf("Value %d : %d\n", i + 1, ptr[i]);
    }

    /* Free memory */
    free(ptr);
    ptr = NULL;

    return 0;
}

Output:

Enter how many numbers you want to store: 2

Enter 2 values:

50

30

Stored values:

Value 1 : 50

Value 2 : 30

3. realloc() क्या है?

यह Function तब उपयोग किया जाता है जब हम पहले से malloc() या calloc() की मदद से memory allocate कर चुके हों, लेकिन बाद में उस memory का size बदलने की आवश्यकता पड़ जाए।

मान लीजिए कि शुरू में हमने 5 integer values को store करने के लिए memory allocate की थी। लेकिन program में आगे चलकर हमें महसूस हुआ कि अब सिर्फ 3 या 2 values ही store करनी हैं, या फिर कभी-कभी ज्यादा values की जरूरत पड़ सकती है।

ऐसी situation में हमें पुरानी memory को free करने की जरूरत नहीं होती और न ही पहले से store की गई values overwrite होती हैं। हम सीधे realloc() function का use करके उसी memory का size runtime पर कम या ज्यादा कर सकते हैं।

Example:

ptr = realloc(ptr, 10 * sizeof(int));

realloc Dynamic Memory Allocation in C using realloc() Function

#include <stdio.h>
#include <stdlib.h>
int main()
{
/*
This program demonstrates BASIC concept of
Dynamic Memory Allocation in C using
malloc() and realloc()
*/
int *ptr, n, oldSize, i;
// Taking initial number of elements
printf("Enter how many numbers you want to store: ");
scanf("%d", &n);
/*
malloc() dynamically allocates memory at runtime
Memory is allocated in Heap
*/
ptr = (int*) malloc(n * sizeof(int));
// Check whether memory allocation is successful
if(ptr == NULL)
{
printf("Memory allocation failed");
return 0;
}
// Storing values in dynamically allocated memory
for(i = 0; i < n; i++)
{
printf("Enter value %d: ", i + 1);
scanf("%d", &ptr[i]);
}
// Displaying stored values
printf("\nStored values:\n");
for(i = 0; i < n; i++)
{
printf("Value at index %d = %d\n", i, ptr[i]);
}
oldSize = n;
// Taking additional number of elements
printf("\nEnter additional number of values: ");
scanf("%d", &n);
n = oldSize + n;
/*
realloc() changes the size of already allocated memory
Old data remains safe
New memory is added in Heap
*/
ptr = realloc(ptr, n * sizeof(int));
// Checking reallocation success
if(ptr == NULL)
{
printf("Memory reallocation failed");
return 0;
}
// Storing new values in extended memory
for(i = oldSize; i < n; i++)
{
printf("Enter new value %d: ",i);
scanf("%d", &ptr[i]);
}
// Displaying final values
printf("\nFinal values after reallocation:\n");
for(i = 0; i < n; i++)
{
printf("Value at index %d = %d\n", i, ptr[i]);
}
/*
free() releases dynamically allocated memory
Prevents memory leak
*/
free(ptr);
ptr = NULL;
return 0;
} 

Output:

Enter how many numbers you want to store: 2

Enter value 1: 5

Enter value 2: 4

Stored values:

Value at index 0 = 5

Value at index 1 = 4

Enter additional number of values: 3

Enter new value 2: 3

Enter new value 3: 2

Enter new value 4: 1

Final values after reallocation:

Value at index 0 = 5

Value at index 1 = 4

Value at index 2 = 3

Value at index 3 = 2

Value at index 4 = 1

4. free() क्या है और क्यों जरूरी है?

free() Function का उपयोग उस Memory को ‘De-allocate’ (वापस लौटाने) के लिए किया जाता है जिसे हमने malloc, calloc या realloc के ज़रिए Program चलते समय (Runtime) Reserve किये थे।

Syntax:

free(ptr);

C Language में Dynamic Memory Allocation क्यों इस्तेमाल करते हैं?

जब हम int a[100] लिखते हैं, तो यह Memory Fix हो जाती है। इसे हम Program के बीच में बदल नहीं सकते। Dynamic Memory हमें आज़ादी देती है कि हम अपनी मर्जी से Memory कम या ज्यादा कर सकें।

Dynamic Memory Allocation के फायदे

  • Runtime memory allocation: आप Program चलते समय तय कर सकते हैं कि आपको कितनी Memory चाहिए।
  • Memory wastage कम: अगर Data कम है, तो कम Memory यूज़ करें। फालतू Memory Block नहीं होती।
  • Large data handling: बड़े Data Structures जैसे Linked List या Trees बनाने के लिए यह बहुत ज़रूरी है।

Dynamic Memory Allocation के नुकसान

  • Memory leak risk: अगर आपने Memory ली और उसे वापस नहीं किया (free), तो Computer की Memory भरती जाएगी जिससे Memory Leak होने का खतरा बढ़ जाता है।
  • Pointer errors: इसमें सारा काम pointers के ज़रिए होता है, तो थोड़ी सी गलती से Program Crash हो सकता है।
  • Manual memory management: यहाँ आपको खुद ही Memory माँगनी पड़ती है और खुद ही वापस (free) करनी पड़ती है।

malloc vs calloc vs realloc (Comparison Table)

इस Table malloc(), calloc(), aur realloc() Function में Difference बताया गया है : 

FunctionकामInitializationSpeed
mallocएक Single Block देनाGarbage value (कचरा)Fast
callocकई Block देनाZero (0)थोड़ी Slow
reallocसाइज़ कम या ज्यादा करनापिछला डेटा बना रहता हैMedium

Common Errors (शुरुआती गलतियाँ)

  1. free() भूल जाना: इससे “Memory Leak” होता है। आपका Program बंद होने के बाद भी RAM घेरे रहता है।
  2. NULL pointer check न करना: हमेशा चेक करें if (ptr == NULL), क्योंकि कभी-कभी RAM खाली नहीं होती।
  3. Dangling pointer: free(ptr) करने के बाद भी अगर आप ptr को यूज़ करने की कोशिश करेंगे, तो Program क्रैश हो जाएगा।

Quizzes

Dynamic Memory Allocation Quiz (C)

Q1. malloc() का return type क्या होता है?

A) int  
B) char*  
C) void*  
D) float
Answer: C Reason: malloc() void* return करता है, जिसे किसी भी data type के pointer में typecast किया जा सकता है।

Q2. calloc() में default value क्या होती है?

A) Garbage value  
B) 1  
C) NULL  
D) 0
Answer: D Reason: calloc() allocated memory के सभी bytes को default रूप से 0 से initialize करता है।

Q3. realloc() का इस्तेमाल कब किया जाता है?

A) नई memory allocate करने के लिए  
B) Memory free करने के लिए  
C) Existing memory का size change करने के लिए  
D) Stack memory बनाने के लिए
Answer: C Reason: realloc() पहले से allocated memory का size runtime पर बढ़ाने या घटाने के लिए use किया जाता है।

Q4. Dynamic Memory किस area में store होती है?

A) Stack  
B) Heap  
C) Register  
D) Cache
Answer: B Reason: Dynamic Memory Allocation हमेशा Heap memory में होती है।

Q5. free() function किस header file में होता है?

A) stdio.h  
B) string.h  
C) stdlib.h  
D) math.h
Answer: C Reason: malloc(), calloc(), realloc() और free() सभी stdlib.h header file में declared होते हैं।

FAQs

Q1. Dynamic Memory Allocation क्या है?

Ans: Program के runtime के दौरान जरूरत के हिसाब से memory को allocate और manage करने की technique को Dynamic Memory Allocation कहते हैं।

इसमें memory compile time पर fix नहीं होती, बल्कि program चलते समय ली जाती है।

Q2. malloc और calloc में क्या फर्क है?

Ans: malloc() एक single block of memory allocate करता है और उसमें garbage value होती है।

जबकि calloc() multiple blocks allocate करता है और सारी memory को 0 से initialize करता है।

Q3. realloc कब use करते हैं?

Ans: जब पहले से malloc() या calloc() से allocate की गई memory का size बाद में बढ़ाना या घटाना हो, तब realloc() का use किया जाता है।

Q4. free() जरूरी क्यों है?

Ans: free() का use allocated memory को वापस RAM में release करने के लिए किया जाता है।

अगर हम free() नहीं लिखते, तो program में Memory Leak हो सकता है।

Q5. C में Memory Leak कैसे रोकें?

Ans: हमेशा यह सुनिश्चित करें कि हर malloc() या calloc() के लिए एक matching free() जरूर लिखा गया हो।

यही memory leak से बचने का सबसे सही तरीका है।

Conclusion (निष्कर्ष)

आज के इस ब्लॉग में आपने सीखा कि C programming में Dynamic Memory Allocation केवल एक Topic नहीं, बल्कि एक Powerful tool  है। यह हमें एक “Smart Programmer” बनाता है क्योंकि इसके जरिए हम Computer की Memory (RAM) का सही इस्तेमाल करना सीखते हैं।

याद रखने योग्य सबसे महत्वपूर्ण बात: Dynamic Memory Allocation एक बड़ी जिम्मेदारी के साथ आता है। नियम सीधा है— “जितनी मेमोरी उधार लो, उसे काम खत्म होने पर free()Function के जरिए वापस लौटायें।” अगर आप इस एक नियम का पालन करते हैं, तो आप ‘Memory Leak’ जैसी गंभीर समस्याओं से बच सकते हैं और आपके Program की Performance हमेशा बेहतरीन बनी रहेगी।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top