91-9990449935 0120-4256464 |
Dynamic memory allocation in CThe concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.
Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation.
Now let's have a quick look at the methods used for dynamic memory allocation.
malloc() function in CThe malloc() function allocates single block of requested memory. It doesn't initialize memory at execution time, so it has garbage value initially. It returns NULL if memory is not sufficient. The syntax of malloc() function is given below: Let's see the example of malloc() function. Output: Enter elements of array: 3 Enter elements of array: 10 10 10 Sum=30 calloc() function in CThe calloc() function allocates multiple block of requested memory. It initially initialize all bytes to zero. It returns NULL if memory is not sufficient. The syntax of calloc() function is given below: Let's see the example of calloc() function. Output: Enter elements of array: 3 Enter elements of array: 10 10 10 Sum=30 realloc() function in CIf memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc() function. In short, it changes the memory size. Let's see the syntax of realloc() function. free() function in CThe memory occupied by malloc() or calloc() functions must be released by calling free() function. Otherwise, it will consume memory until program exit. Let's see the syntax of free() function.
Next TopicStructure in C
|