Dynamic Memory Allocation in C in Hindi – malloc, calloc, realloc और free की पूरी जानकारी

क्या आप C programming सीख रहे हैं और Dynamic Memory Allocation in C in Hindi में step-by-step समझना चाहते हैं? तो यह post बिल्कुल आपके लिए है। बहुत से beginners को यह topic थोड़ा complex लगता है, लेकिन इस post में हम इसे इतनी आसान भाषा में समझाएंगे कि आपको एक बार पढ़ने के बाद दोबारा कहीं जाने की जरूरत नहीं पड़ेगी।

इस post में आप सीखेंगे कि memory allocation क्या होती है, static और dynamic allocation में क्या फर्क है, और C language के चारों important functions — malloc(), calloc(), realloc(), और free() — को real code examples के साथ कैसे use किया जाता है। साथ ही हम common mistakes, Stack vs Heap memory का comparison, और एक complete practical example भी देखेंगे।

यह guide उन सभी के लिए है जो C programming में नए हैं या जिन्होंने यह topic पहले पढ़ा है लेकिन अभी भी confuse हैं। हर section में code examples दिए गए हैं ताकि आप सीधे practice शुरू कर सकें। तो चलिए शुरू करते हैं।

Memory Allocation क्या होती है? (What is Memory Allocation?)

जब कोई C program run होता है, तो उसे computer की RAM में जगह चाहिए होती है — variables store करने के लिए, calculations के लिए, और data रखने के लिए। यह जगह देने की process को Memory Allocation कहते हैं। C में यह दो तरह से होती है।

1. Static Memory Allocation

जब हम program लिखते समय ही किसी variable या array का size पहले से fix कर देते हैं, उसे Static Memory Allocation कहते हैं।

int arr[10]; // size compile time पर fix हो गया

इसमें एक बड़ी problem है — अगर user को सिर्फ 3 numbers store करने हों, तो भी 10 slots की memory बर्बाद होगी। और अगर 15 numbers चाहिए, तो program crash हो सकता है।

2. Dynamic Memory Allocation

जब memory program के runtime पर (यानी program चलते समय) user की जरूरत के हिसाब से allocate की जाती है, उसे Dynamic Memory Allocation कहते हैं। यह memory Heap section में allocate होती है, जो Stack से बड़ी और flexible होती है।

Dynamic Memory Allocation in C क्यों जरूरी है?

स्थितिStatic AllocationDynamic Allocation
Size पहले से पता होठीक हैठीक है
Size runtime पर पता चलेProblemBest Solution
Memory waste होती है?हाँ, बहुत बारनहीं
Flexibilityकमज्यादा
Use caseSimple programsReal-world apps

Real-world applications जैसे databases, linked lists, और file systems में data का size पहले से पता नहीं होता। इसीलिए Dynamic Memory Allocation in C इतनी जरूरी है।

C में Dynamic Memory Allocation के 4 Functions

C language में dynamic memory के लिए <stdlib.h> header file में ये 4 functions दिए गए हैं:

Functionकाम
malloc()Memory allocate करता है (garbage values के साथ)
calloc()Memory allocate करता है (zero से initialize)
realloc()पहले से ली हुई memory का size बदलता है
free()Allocated memory को release करता है

malloc() Function in C Hindi

malloc का full form है — Memory ALLOCation

Syntax

pointer = (datatype*) malloc(size_in_bytes);

malloc() कैसे काम करता है?

  • यह heap में specified bytes की memory allocate करता है
  • यह memory initialize नहीं करता — garbage values हो सकती हैं
  • अगर memory available नहीं है, तो NULL return करता है
  • हमेशा NULL check करना जरूरी है

malloc() का Example Program 

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

int main() {
    int *arr;   // Pointer to store the address of dynamically allocated memory
    int n;

    printf("How many numbers do you want to store? ");
    scanf("%d", &n);

    // Dynamically allocate memory for n integers
    arr = (int *)malloc(n * sizeof(int));

    // Check whether memory allocation was successful
    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Read array elements
    for (int i = 0; i < n; i++) {
        printf("arr[%d] = ", i);
        scanf("%d", &arr[i]);
    }

    // Display array elements
    printf("You entered: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    // Release the allocated memory
    free(arr);

    return 0;
}
▶️ Output Result
How many numbers do you want to store? 3 arr[0] = 10 arr[1] = 20 arr[2] = 30 You entered: 10 20 30

calloc() Function in C Hindi

calloc का full form है — Contiguous ALLOCation

malloc() और calloc() में अंतर

Featuremalloc()calloc()
Parametersएक (total bytes)दो (elements, size each)
Initializationनहीं (garbage values)हाँ (सब 0 से initialize)
Speedथोड़ा तेज़थोड़ा slow

Syntax

pointer = (datatype*) calloc(number_of_elements, size_of_each);

calloc() का Example Program

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

int main() {
    int n = 5;   // Number of integers

    // Allocate memory for 5 integers and initialize all values to 0
    int *arr = (int *)calloc(n, sizeof(int));

    // Check whether memory allocation was successful
    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Display the default values
    printf("Default values (calloc): ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    // Update some array elements
    arr[0] = 100;
    arr[1] = 200;

    // Display the updated values
    printf("\nUpdated values: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    // Release the allocated memory
    free(arr);

    return 0;
}
▶️ Output Result
Default values (calloc): 0 0 0 0 0 Updated values: 100 200 0 0 0

realloc() Function in C Hindi

realloc का full form है — RE-ALLOCation

realloc() क्यों use करते हैं?

मान लो आपने पहले 3 integers के लिए memory ली, लेकिन बाद में 5 की जरूरत पड़ी। ऐसे में realloc() से memory का size बढ़ाया या घटाया जा सकता है — और पुरानी values safe रहती हैं।

Syntax

pointer = (datatype*) realloc(pointer, new_size_in_bytes);

realloc() का Example Program

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

int main() {
    // Allocate memory for 3 integers
    int *arr = (int *)malloc(3 * sizeof(int));

    // Check whether memory allocation was successful
    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Store initial values
    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;

    printf("First 3 values: %d %d %d\n", arr[0], arr[1], arr[2]);

    // Increase the array size from 3 to 5 integers
    arr = (int *)realloc(arr, 5 * sizeof(int));

    // Check whether reallocation was successful
    if (arr == NULL) {
        printf("Memory reallocation failed!\n");
        return 1;
    }

    // Store values in the newly allocated memory
    arr[3] = 40;
    arr[4] = 50;

    // Display all array elements
    printf("After realloc (5 values): ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    // Release the allocated memory
    free(arr);

    return 0;
}
▶️ Output Result
First 3 values: 10 20 30 After realloc (5 values): 10 20 30 40 50
int *p = (int*) malloc(sizeof(int));

*p = 50;

printf("%d\n", *p);

free(p);   // Memory release

p = NULL;  // Dangling pointer से बचाव (best practice)

Stack vs Heap Memory – Quick Comparison

Stack Memory और Heap Memory दोनों RAM का हिस्सा होते हैं, लेकिन दोनों का Memory Management, Allocation Method और Use Case अलग-अलग होता है। नीचे दी गई टेबल में इनके मुख्य अंतर को आसान भाषा में समझाया गया है। 

FeatureStack MemoryHeap Memory
AllocationAutomaticManual (malloc, calloc)
SizeLimitedबड़ी और flexible
Speedतेज़थोड़ी slow
Memory releaseAutomaticfree() से manually
Use caseLocal variablesDynamic data structures
Dynamic Memory Allocation in C in Hindi

Dynamic Memory Allocation का Complete Practical Example Program

यह Program Dynamic Memory Allocation के सभी मुख्य Functions—malloc(), realloc() और free()—का एक साथ उपयोग करके दिखाता है। इसमें पहले User से Array का Size लिया जाता है, फिर Memory Allocate की जाती है, Data Store करके Sum और Average निकाला जाता है, उसके बाद realloc() की मदद से Array का Size बढ़ाया जाता है और अंत में free() का उपयोग करके Allocated Memory को Release किया जाता है। 

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

int main() {
    int n;

    printf("How many elements? ");
    scanf("%d", &n);

    // Step 1: Allocate memory for n integers
    int *arr = (int *)malloc(n * sizeof(int));

    // Check whether memory allocation was successful
    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Step 2: Read array elements
    for (int i = 0; i < n; i++) {
        printf("Enter element %d: ", i + 1);
        scanf("%d", &arr[i]);
    }

    // Step 3: Calculate the sum and average
    int sum = 0;

    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }

    printf("Sum = %d\n", sum);
    printf("Average = %.2f\n", (float)sum / n);

    // Step 4: Increase the array size by 2 elements
    int *temp = (int *)realloc(arr, (n + 2) * sizeof(int));

    // Check whether memory reallocation was successful
    if (temp == NULL) {
        printf("Memory reallocation failed!\n");
        free(arr);
        return 1;
    }

    arr = temp;

    // Store values in the new elements
    arr[n] = 999;
    arr[n + 1] = 888;

    // Display the updated array
    printf("Extended Array: ");
    for (int i = 0; i < n + 2; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    // Step 5: Release the allocated memory
    free(arr);
    arr = NULL;

    return 0;
}
▶️ Output Result
How many elements? 3 Enter element 1: 10 Enter element 2: 20 Enter element 3: 30 Sum = 60 Average = 20.00 Extended Array: 10 20 30 999 888

Common Mistakes जो Beginners करते हैं

गलती 1 – NULL Check ना करना: malloc() fail हो सकता है। हमेशा check करें कि return value NULL तो नहीं है, वरना program crash हो सकता है।

गलती 2 – Dangling Pointer use करना: free() के बाद pointer को NULL assign करें। Free हुई memory को access करना undefined behavior है।

गलती 3 – Double Free: एक ही pointer को दो बार free() करने से program crash हो जाता है। इससे बचने के लिए free के बाद p = NULL करें।

गलती 4 – Memory Leak: Memory allocate करके उसे free करना भूल जाना real-world apps में बहुत बड़ी problem है। हर malloc()/calloc() के लिए एक free() होनी चाहिए।

Conclusion ( निष्कर्ष )

इस post में हमने Dynamic Memory Allocation in C in Hindi को पूरी तरह से और आसान भाषा में समझा। हमने देखा कि static allocation की limitations क्यों होती हैं, और runtime पर flexible memory लेने के लिए malloc(), calloc(), realloc(), और free() कैसे काम करते हैं। साथ ही हमने common mistakes और best practices भी cover कीं — जैसे NULL check करना, free के बाद NULL assign करना, और memory leak से बचना। यह सब concepts real-world C programs में हर जगह use होते हैं।

अगर आप C programming को seriously सीखना चाहते हैं, तो इस post के सभी code examples को खुद type करके compile और run करें — सिर्फ पढ़ने से नहीं, practice करने से concepts clear होते हैं। अगर कोई doubt हो या कोई example समझ न आए, तो नीचे comment करें। इस post को अपने दोस्तों के साथ share करें जो C programming सीख रहे हैं, ताकि उन्हें भी फायदा हो।

Frequently Asked Questions

Q 1: Dynamic Memory Allocation और Static Memory Allocation में क्या फर्क है?
Ans: Static allocation में size compile time पर fix होती है और Stack में store होती है। Dynamic allocation में size runtime पर decide होती है और Heap में store होती है। Dynamic allocation ज्यादा flexible होती है और real-world applications के लिए जरूरी होती है।
Q 2: अगर malloc() NULL return करे तो क्या करें?
Ans: इसका मतलब है कि system में उतनी memory available नहीं है जितनी आपने मांगी। ऐसे में program को gracefully handle करना चाहिए — error message print करके safely exit करें। कभी भी NULL pointer को directly use न करें।
Q 3: free() के बाद pointer को NULL क्यों करते हैं?
Ans: free() memory release कर देता है, लेकिन pointer अभी भी उसी address को point करता रहता है — इसे Dangling Pointer कहते हैं। अगर गलती से उसे access किया जाए तो crash होगा। p = NULL करने से यह खतरा खत्म हो जाता है।
Q 4: क्या realloc() पुरानी values को delete कर देता है?
Ans: नहीं। realloc() पुरानी values को नई memory location पर copy करता है और नई जगह add करता है। इसलिए पुराना data safe रहता है। हाँ, अगर size घटाई जाए तो जो data हटाई गई range में था, वह चला जाता है।
Q 5: Memory Leak क्या होती है और यह कैसे होती है?
Ans: जब dynamically allocated memory को free() नहीं किया जाता, तो वह memory program के end तक block रहती है और OS उसे reuse नहीं कर सकता — इसे Memory Leak कहते हैं। Long-running programs में यह system को slow और crash कर सकती है।

Leave a Comment

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

Scroll to Top