Dynamic Memory Allocation in C Language with Examples of malloc(), calloc(), realloc() and free() functions.
In C language, memory can be allocated to variables and arrays in different ways. One important feature of C is Dynamic Memory Allocation, which allows a programmer to allocate and release memory during program execution according to the program's requirements.
C लैंग्वेज में वेरिएबल और Array के लिए अलग-अलग तरीकों से मेमोरी Allocate की जा सकती है। C का एक महत्वपूर्ण फीचर Dynamic Memory Allocation है, जिसकी सहायता से प्रोग्रामर प्रोग्राम की आवश्यकता के अनुसार प्रोग्राम के एक्जीक्यूशन के दौरान मेमोरी Allocate और Release कर सकता है।
There are two commonly discussed approaches to memory allocation:
मेमोरी एलोकेशन के मुख्य रूप से दो तरीकों का अध्ययन किया जाता है:
Static or fixed-size memory allocation
Dynamic memory allocation
1. Static Memory Allocation
When the size of a variable or array is fixed in its declaration and cannot be changed through that declaration during program execution, it is commonly described as static or fixed-size memory allocation.
जब किसी वेरिएबल या Array का Size उसके Declaration के समय निश्चित कर दिया जाता है और प्रोग्राम के एक्जीक्यूशन के दौरान उस Declaration के माध्यम से उसे बदला नहीं जा सकता, तो इसे सामान्यतः Static या Fixed-Size Memory Allocation कहा जाता है।
Example:
int a;float list[5];
In this example, a is an integer variable and list is an array that can store five floating-point values. The declared array size remains fixed.
इस उदाहरण में a एक Integer Variable है और list एक Array है जिसमें पाँच Floating-Point Values स्टोर की जा सकती हैं। डिक्लेअर किए गए Array का Size निश्चित रहता है।
Limitation: If the programmer declares an array of five elements but needs to store twenty elements during execution, the original array cannot simply be enlarged. A different allocation approach is required.
सीमा: यदि प्रोग्रामर पाँच Elements का Array डिक्लेअर करता है, लेकिन प्रोग्राम के एक्जीक्यूशन के दौरान बीस Elements स्टोर करने की आवश्यकता होती है, तो मूल Array का Size सीधे नहीं बढ़ाया जा सकता। इसके लिए दूसरे Memory Allocation Method की आवश्यकता होती है।
2. Dynamic Memory Allocation
Dynamic Memory Allocation is a process of allocating memory during program execution. It allows the programmer to request memory according to the requirements of the program.
Dynamic Memory Allocation वह प्रक्रिया है जिसमें प्रोग्राम के एक्जीक्यूशन के दौरान मेमोरी Allocate की जाती है। इसकी सहायता से प्रोग्रामर प्रोग्राम की आवश्यकता के अनुसार मेमोरी प्राप्त कर सकता है।
In C language, dynamically allocated memory is obtained from the allocated storage area, commonly called the heap. The programmer can allocate, resize and release this memory using standard library functions.
C लैंग्वेज में Dynamic Memory Allocation के लिए मेमोरी सामान्यतः Heap नामक Memory Area से प्राप्त की जाती है। प्रोग्रामर Standard Library Functions की सहायता से इस मेमोरी को Allocate, Resize और Release कर सकता है।
The following four functions are commonly used for dynamic memory allocation in C:
C लैंग्वेज में Dynamic Memory Allocation के लिए सामान्यतः निम्न चार फंक्शन्स का उपयोग किया जाता है:
malloc()calloc()realloc()free()
Header File: These functions are declared in the standard header file <stdlib.h>.
ये सभी फंक्शन्स Standard Header File <stdlib.h> में डिक्लेअर किए गए हैं।
a) malloc() Function
The malloc() function stands for Memory Allocation. It allocates a specified number of bytes of memory during program execution. If memory allocation is successful, it returns a pointer to the beginning of the allocated memory. Otherwise, it returns NULL.
malloc() का पूरा नाम Memory Allocation है। यह प्रोग्राम के एक्जीक्यूशन के दौरान निर्धारित संख्या में Memory Bytes Allocate करता है। यदि Memory Allocation सफल होता है, तो यह Allocate की गई मेमोरी के पहले Byte का Address Return करता है। अन्यथा यह NULL Return करता है।
Syntax:
void *malloc(size_t size);Here, size represents the total number of bytes to be allocated.
यहाँ size का अर्थ Allocate की जाने वाली कुल Memory Bytes की संख्या से है।
Example:
int *p;p = malloc(5 * sizeof(int));
In this example, memory sufficient to store five integer values is allocated dynamically. The returned address is stored in pointer p.
इस उदाहरण में पाँच Integer Values को स्टोर करने के लिए पर्याप्त मेमोरी Dynamically Allocate की जाती है। Return किया गया Address Pointer p में स्टोर होता है।
Important Points:
malloc()allocates memory in bytes.The allocated memory is uninitialized.
It returns
NULLif allocation fails.The allocated memory should be released using
free()when it is no longer required.
महत्वपूर्ण बातें:
malloc()मेमोरी को Bytes में Allocate करता है।Allocate की गई मेमोरी Uninitialized होती है।
यदि Memory Allocation असफल होता है, तो यह
NULLReturn करता है।आवश्यकता समाप्त होने पर Allocate की गई मेमोरी को
free()द्वारा Release करना चाहिए।
b) calloc() Function
The calloc() function stands for Contiguous Allocation. It allocates memory for a specified number of elements, each having a specified size. It initializes all allocated bytes to zero.
calloc() का पूरा नाम Contiguous Allocation है। यह निर्धारित संख्या में Elements के लिए मेमोरी Allocate करता है, जहाँ प्रत्येक Element का Size भी निर्धारित किया जाता है। यह Allocate किए गए सभी Bytes को Zero से Initialize करता है।
Syntax:
void *calloc(size_t num, size_t size);Here:
numrepresents the number of elements.sizerepresents the size of each element in bytes.
यहाँ:
numका अर्थ Elements की संख्या से है।sizeका अर्थ प्रत्येक Element के Size (Bytes में) से है।
Example:
float *p;p = calloc(4, sizeof(float));
In this example, memory for four floating-point elements is allocated dynamically, and all allocated bytes are initialized to zero.
इस उदाहरण में चार Floating-Point Elements के लिए मेमोरी Dynamically Allocate की जाती है तथा Allocate किए गए सभी Bytes को Zero से Initialize किया जाता है।
Important Points:
calloc()allocates memory for multiple elements.It initializes all allocated bytes to zero.
It returns
NULLif memory allocation fails.The allocated memory should be released using
free().
महत्वपूर्ण बातें:
calloc()अनेक Elements के लिए मेमोरी Allocate करता है।यह Allocate किए गए सभी Bytes को Zero से Initialize करता है।
Memory Allocation असफल होने पर यह
NULLReturn करता है।Allocate की गई मेमोरी को
free()द्वारा Release करना चाहिए।
c) realloc() Function
The realloc() function stands for Reallocation. It is used to change the size of a previously dynamically allocated memory block. It may expand or shrink the allocated memory block.
realloc() का पूरा नाम Reallocation है। इसका उपयोग पहले से Dynamically Allocate किए गए Memory Block का Size बदलने के लिए किया जाता है। इसकी सहायता से Memory Block का Size बढ़ाया या घटाया जा सकता है।
The function preserves the existing data up to the smaller of the old and new sizes. However, it may move the memory block to a new location. If reallocation fails for a non-zero requested size, the original memory block remains valid.
यह फंक्शन पुराने और नए Size में से छोटे Size तक पुराने Data को सुरक्षित रखता है। हालाँकि, यह Memory Block को किसी नए स्थान पर भी स्थानांतरित कर सकता है। यदि Non-Zero Size के लिए Reallocation असफल होता है, तो पुराना Memory Block सुरक्षित रहता है।
Syntax:
void *realloc(void *ptr, size_t newsize);Here:
ptris a pointer to previously allocated memory.newsizerepresents the new memory size in bytes.
यहाँ:
ptrपहले से Allocate की गई मेमोरी का Pointer है।newsizeनई मेमोरी का Size Bytes में दर्शाता है।
Example:
int *p;int *temp;p = malloc(5 * sizeof(int));if (p != NULL){temp = realloc(p, 10 * sizeof(int));if (temp != NULL){p = temp;}}
In this example, the allocated memory is initially sufficient for five integers. The realloc() function attempts to resize it to hold ten integers. A temporary pointer is used so that the original allocation is not lost if reallocation fails.
इस उदाहरण में शुरुआत में पाँच Integers के लिए पर्याप्त मेमोरी Allocate की जाती है। realloc() फंक्शन इसे दस Integers के लिए Resize करने का प्रयास करता है। Temporary Pointer का उपयोग किया गया है ताकि Reallocation असफल होने पर पुरानी मेमोरी का Address खो न जाए।
Important Points:
realloc()can increase or decrease the allocated memory size.Existing data is preserved up to the smaller of the old and new sizes.
The memory address may change after reallocation.
Always use a temporary pointer when storing the result of
realloc().
महत्वपूर्ण बातें:
realloc()Allocate की गई मेमोरी का Size बढ़ा या घटा सकता है।पुराने और नए Size में से छोटे Size तक का Data सुरक्षित रहता है।
Reallocation के बाद Memory Address बदल सकता है।
realloc()का Result Store करने के लिए हमेशा Temporary Pointer का उपयोग करना चाहिए।
d) free() Function
The free() function is used to release dynamically allocated memory that is no longer required by the program. It returns the allocated memory to the memory management system so that it can be reused.
free() फंक्शन का उपयोग उस Dynamically Allocated Memory को Release करने के लिए किया जाता है जिसकी अब प्रोग्राम को आवश्यकता नहीं है। यह Allocate की गई मेमोरी को Memory Management System के पास वापस कर देता है ताकि उसका दोबारा उपयोग किया जा सके।
Syntax:
void free(void *ptr);Here, ptr is a pointer to memory previously allocated by functions such as malloc(), calloc() or realloc().
यहाँ ptr उस मेमोरी का Pointer है जिसे पहले malloc(), calloc() या realloc() जैसे फंक्शन्स द्वारा Allocate किया गया हो।
Example:
float *p;p = calloc(4, sizeof(float));if (p != NULL){/* Statements using allocated memory */free(p);p = NULL;}
In this example, memory for four floating-point values is allocated using calloc(). After completing the required operations, free() releases the allocated memory.
इस उदाहरण में calloc() की सहायता से चार Floating-Point Values के लिए मेमोरी Allocate की जाती है। आवश्यक कार्य पूरा होने के बाद free() Allocate की गई मेमोरी को Release कर देता है।
Important Points:
free()releases dynamically allocated memory.It does not return a value.
free(NULL)is safe and does nothing.After releasing memory, the pointer should not be used to access that memory.
Assigning
NULLto the pointer after freeing it helps prevent accidental use through that pointer.
महत्वपूर्ण बातें:
free()Dynamically Allocated Memory को Release करता है।यह कोई Value Return नहीं करता है।
free(NULL)सुरक्षित है और कोई कार्य नहीं करता।मेमोरी Release करने के बाद उस Pointer के माध्यम से उस मेमोरी को Access नहीं करना चाहिए।
Memory Free करने के बाद Pointer को
NULLAssign करने से उस Pointer के माध्यम से गलती से मेमोरी Access करने की संभावना कम होती है।
Note: C language does not have an automatic garbage collector. The programmer is responsible for releasing dynamically allocated memory when it is no longer needed. Failure to release memory can cause a memory leak.
नोट: C लैंग्वेज में Automatic Garbage Collector उपलब्ध नहीं होता है। आवश्यकता समाप्त होने पर Dynamically Allocated Memory को Release करना प्रोग्रामर की जिम्मेदारी होती है। मेमोरी Release नहीं करने पर Memory Leak की समस्या हो सकती है।
Difference Between malloc() and calloc()
| malloc() | calloc() |
|---|---|
| Allocates a specified number of bytes. | Allocates memory for a specified number of elements. |
| Takes one argument. | Takes two arguments. |
| Allocated bytes are uninitialized. | Allocated bytes are initialized to zero. |
malloc(size) | calloc(num, size) |
हिंदी में अंतर:
| malloc() | calloc() |
|---|---|
| निर्धारित संख्या में Bytes Allocate करता है। | निर्धारित संख्या में Elements के लिए मेमोरी Allocate करता है। |
| एक Argument लेता है। | दो Arguments लेता है। |
| Allocate किए गए Bytes Uninitialized होते हैं। | Allocate किए गए Bytes Zero से Initialize होते हैं। |
malloc(size) | calloc(num, size) |
C Program for Dynamic Memory Allocation Using malloc()
The following program dynamically allocates memory for a string, accepts a string from the user and displays it.
निम्न प्रोग्राम में String के लिए Dynamically Memory Allocate की जाती है, यूजर से String इनपुट ली जाती है और उसे Display किया जाता है।
Program:
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(void){char *str;str = malloc(100 * sizeof(char));if (str == NULL){printf("Memory allocation failed.\n");return 1;}printf("Enter a string: ");if (fgets(str, 100, stdin) != NULL){str[strcspn(str, "\n")] = '\0';printf("String is: ");puts(str);}free(str);str = NULL;return 0;}
Output:
Enter a string: Welcome to C ProgrammingString is: Welcome to C Programming
Explanation:
The program includes
<stdio.h>,<stdlib.h>and<string.h>header files.प्रोग्राम में
<stdio.h>,<stdlib.h>तथा<string.h>Header Files को Include किया गया है।A character pointer
stris declared to store the address of dynamically allocated memory.Dynamically Allocate की गई मेमोरी का Address Store करने के लिए Character Pointer
strडिक्लेअर किया गया है।The
malloc()function allocates memory for 100 characters.malloc()फंक्शन 100 Characters के लिए मेमोरी Allocate करता है।The program checks whether memory allocation is successful by checking
stragainstNULL.प्रोग्राम
strकोNULLसे Compare करके यह चेक करता है कि Memory Allocation सफल हुआ है या नहीं।The
fgets()function accepts the string from the user. It is used instead of the unsafegets()function.fgets()फंक्शन यूजर से String इनपुट लेता है। इसका उपयोग Unsafegets()फंक्शन के स्थान पर किया गया है।The
strcspn()function helps remove the newline character from the input string.strcspn()फंक्शन Input String से Newline Character हटाने में सहायता करता है।Finally,
free()releases the dynamically allocated memory.अंत में
free()Dynamically Allocate की गई मेमोरी को Release करता है।
Dynamic Memory Allocation is an important feature of C language. It allows programmers to allocate memory according to program requirements during execution. The four important functions used for this purpose are malloc(), calloc(), realloc() and free().
Dynamic Memory Allocation C लैंग्वेज का एक महत्वपूर्ण फीचर है। इसकी सहायता से प्रोग्रामर प्रोग्राम के एक्जीक्यूशन के दौरान आवश्यकता के अनुसार मेमोरी Allocate कर सकता है। इसके लिए उपयोग किए जाने वाले चार महत्वपूर्ण फंक्शन्स malloc(), calloc(), realloc() और free() हैं।
Comments
Post a Comment