क्या आप C programming सीख रहे हैं और Dynamic Memory Allocation in C in Hindi में समझना चाहते हैं? तो आप बिल्कुल सही जगह आए हैं। इस post में हम step-by-step, बिल्कुल आसान भाषा में समझेंगे कि dynamic memory allocation क्या होती है, यह क्यों जरूरी है, और इसे C program में कैसे use करते हैं। साथ ही हम malloc, calloc, realloc और free जैसे important functions को code examples के साथ देखेंगे।
Memory Allocation क्या होती है? (What is Memory Allocation?)
जब हम कोई C program लिखते हैं, तो उस program को run होने के लिए computer की memory (RAM) की जरूरत पड़ती है। यह memory दो तरह से allocate होती है:
1. Static Memory Allocation (स्टैटिक मेमोरी)
जब हम program लिखते समय ही variable का size fix कर देते हैं, तो उसे Static Memory Allocation कहते हैं।
Example:
int arr[10]; // array का size पहले से fix है – 10
यहाँ problem यह है कि अगर user को 5 numbers store करने हों, तो भी 10 की memory waste होगी। और अगर 15 numbers चाहिए तो program crash हो सकता है।
2. Dynamic Memory Allocation (डायनामिक मेमोरी)
जब memory program के runtime पर (यानी program चलते समय) allocate की जाती है, उसे Dynamic Memory Allocation कहते हैं।
इसमें हम user की जरूरत के हिसाब से memory ले सकते हैं – न ज्यादा, न कम। यह memory Heap section में allocate होती है।

Dynamic Memory Allocation क्यों जरूरी है? (Why is it Important?)
| स्थिति | Static Allocation | Dynamic Allocation |
|---|---|---|
| Size पहले से पता हो | ✅ ठीक है | ✅ ठीक है |
| Size runtime पर पता चले | ❌ Problem | ✅ Best Solution |
| Memory waste होती है? | ❌ हाँ, बहुत बार | ✅ नहीं, exact लेते हैं |
| Flexibility | ❌ कम | ✅ ज्यादा |
| Use case | Simple programs | Real-world applications |
C में Dynamic Memory Allocation के Functions
C language में dynamic memory allocation के लिए <stdlib.h> header file में 4 main functions दिए गए हैं:
| Function | काम |
|---|---|
| malloc() | Memory allocate करता है (zero से initialize) |
| calloc() | Memory allocate करता है (zero से initialize) |
| realloc() | पहले से allocate memory का size बदलता है |
| free() | Allocate memory को release करता है |
malloc() Function in C Hindi
malloc का full form है – Memory ALLOCation
Syntax:
pointer = (datatype*) malloc(size_in_bytes);
malloc() कैसे काम करता है?
- यह heap memory में specified bytes की memory allocate करता है
- यह memory initialize नहीं करता (garbage values हो सकती हैं)
- अगर memory available नहीं है, तो यह NULL return करता है
malloc() Example in C:
#include <stdio.h>
#include <stdlib.h> // Required for malloc and free functions
int main() {
int n;
printf("How many numbers do you want to store? ");
scanf("%d", &n);
// Allocate memory dynamically for n integers
int *arr = (int*) malloc(n * sizeof(int));
// Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Take input values
for (int i = 0; i < n; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
// Print the entered values
printf("You entered: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Free the allocated memory (very important!)
free(arr);
return 0;
}
Output (example):
How many numbers do you want to store?
arr[0] = 10
arr[1] = 20
arr[2] = 30
You entered: 10 20 30
calloc() Function in C Hindi
calloc का full form है – Contiguous ALLOCation
calloc() और malloc() में अंतर
| Feature | malloc() | calloc() |
|---|---|---|
| Parameters | एक (total bytes) | दो (elements, size each) |
| Initialization | नहीं (garbage | हाँ (सब 0 से initialize) |
| Speed | थोड़ा तेज़ | थोड़ा slow (0 fill की वजह से) |
Syntax:
pointer = (datatype*) calloc(number_of_elements, size_of_each);
calloc() Example in C:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5;
// Allocate memory for 5 integers, all initialized to 0
int *arr = (int*) calloc(n, sizeof(int));
// Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Because of calloc, all values will be 0
printf("Default values (from calloc): ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]); // Output: 0 0 0 0 0
}
printf("\n");
// Now assign values
arr[0] = 100;
arr[1] = 200;
printf("Updated values: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Don't forget to free memory
free(arr);
return 0;
}
Output:
Default values (from 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 करते हैं?
मान लो आपने पहले 5 integers के लिए memory ली, लेकिन बाद में जरूरत पड़ी 10 integers की। ऐसे में realloc() से memory का size बढ़ा या घटा सकते हैं।
Syntax:
#include <stdio.h>
#include <stdlib.h>
int main() {
// Allocate memory for 3 integers
int *arr = (int*) malloc(3 * sizeof(int));
// Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
printf("First 3 values: %d %d %d\n", arr[0], arr[1], arr[2]);
// Increase size from 3 to 5 integers
arr = (int*) realloc(arr, 5 * sizeof(int));
// Check if reallocation was successful
if (arr == NULL) {
printf("Memory reallocation failed!\n");
return 1;
}
// Add new values
arr[3] = 40;
arr[4] = 50;
printf("After resizing to 5 values: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
// Free memory
free(arr);
return 0;
}
Output:
First 3 values: 10 20 30
After resizing to 5 values: 10 20 30 40 50
Note: realloc() पुरानी values को safe रखता है और नई memory attach करता है।
free() Function in C – Memory Leak से बचाव
free() function dynamically allocated memory को release करता है।
free() क्यों जरूरी है?
अगर आप memory allocate करके free नहीं करते, तो वह memory program के end तक block रहती है। इसे Memory Leak कहते हैं, जो real-world applications में बहुत बड़ी problem बन सकती है।
Syntax:
free(pointer);
Memory Leak का Example (गलत तरीका):
// ❌ गलत – memory free नहीं की
int *p = (int*) malloc(sizeof(int));
*p = 50;
// free(p); <– यह नहीं लिखा, Memory Leak!
सही तरीका:
// ✅ सही – memory use के बाद free की
int *p = (int*) malloc(sizeof(int));
*p = 50;
printf(“%d\n”, *p);
free(p); // Memory release
p = NULL; // Pointer को NULL करना best practice है
Dynamic Memory Allocation का Practical Example – Dynamic Array
यह एक complete example है जो सभी concepts को एक साथ use करता है:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("How many elements do you want in the array? ");
scanf("%d", &n);
// Step 1: Allocate memory using malloc
int *arr = (int*) malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory error!\n");
return 1;
}
// Step 2: Take input values
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Step 3: Calculate 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: Add 2 extra elements using realloc
arr = (int*) realloc(arr, (n + 2) * sizeof(int));
arr[n] = 999;
arr[n + 1] = 888;
printf("Extended Array: ");
for (int i = 0; i < n + 2; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Step 5: Free memory
free(arr);
arr = NULL;
return 0;
}
Output:
How many elements do you want in the array? 3
Element 1: 10
Element 2: 20
Element 3: 30
Sum = 60
Average = 20.00
Extended Array: 10 20 30 999 888
Common Mistakes in Dynamic Memory Allocation
गलती 1 – NULL Check ना करना
// गलत
int *p = (int*) malloc(sizeof(int));
*p = 10; // Crash हो सकता है अगर malloc fail हो जाए
// सही
int *p = (int*) malloc(sizeof(int));
if (p != NULL) {
*p = 10;
}
गलती 2 – Dangling Pointer
int *p = (int*) malloc(sizeof(int));
free(p);
// *p = 5; – यह dangling pointer use है, crash होगा
p = NULL; // ✅ free के बाद NULL करो
गलती 3 – Double Free
int *p = (int*) malloc(sizeof(int));
free(p);
free(p); // Double free – undefined behavior
Stack vs Heap Memory – Quick Comparison
| Feature | Stack Memory | Heap Memory |
|---|---|---|
| Allocation | Automatic (compiler करता है) | Manual (programmer करता है) |
| Size | Limited (छोटी) | बड़ी और flexible |
| Speed | तेज़ | थोड़ी slow |
| Functions used | नहीं | malloc, calloc, realloc |
| Memory release | Automatic | free() से manually |
| Use case | Local variables | Dynamic data structures |
निष्कर्ष (Conclusion)
इस post में हमने Dynamic Memory Allocation in C in Hindi को पूरी तरह समझा। हमने सीखा कि:
- Static allocation में size पहले से fix होती है, जबकि dynamic allocation में runtime पर memory ली जाती है।
- malloc() uninitialized memory देता है, जबकि calloc() zero-initialized memory देता है।
- realloc() से पहले से ली हुई memory का size बदला जा सकता है।
- free() से memory release करना जरूरी है, वरना memory leak होती है।
- हमेशा NULL check करो और free के बाद pointer को NULL करो।
Dynamic memory allocation C programming का एक बहुत important concept है, जो real-world applications जैसे linked list, trees, databases आदि में हर जगह use होता है। इसे अच्छे से practice करें।
FAQs – अक्सर पूछे जाने वाले सवाल
Q1. Dynamic Memory Allocation और Static Memory Allocation में क्या फर्क है?
Ans: Static allocation में size compile time पर fix होती है, जबकि dynamic allocation में size runtime पर decide होती है। Dynamic allocation ज्यादा flexible होती है।
Q2. अगर malloc() NULL return करे तो क्या करें?
Ans: इसका मतलब है system में उतनी memory available नहीं है। ऐसे में program को gracefully exit करना चाहिए और user को error message दिखाना चाहिए।
Q3. free() के बाद pointer को NULL क्यों करते हैं?
Ans: free() के बाद भी pointer उसी address को point करता रहता है (dangling pointer)। NULL करने से accidentally उस address को access करने से बचाव होता है।
Q4. क्या realloc() पुरानी values को delete कर देता है?
Ans: नहीं, realloc() पुरानी सारी values को नई memory में copy कर लेता है। सिर्फ extra space नया होता है।
Q5. Memory Leak क्या होती है और यह कैसे होती है?
Ans: जब dynamically allocated memory को free() नहीं किया जाता, तो वह memory program के end तक blocked रहती है। इसे memory leak कहते हैं। यह बड़े programs में performance issues और crashes का कारण बन सकती है।
Q6. क्या dynamic memory allocation सिर्फ arrays के लिए होती है?
Ans: नहीं, यह structures, linked lists, trees, graphs और कई अन्य data structures के लिए भी use होती है।
