Tuesday 30 January 2018

Dynamic Memory Allocation in C

Dynamic Memory Allocation in C

             Dynamic memory management is the process of allocating and freeing up memory during the execution of the program. The declaration char values[ 50 ] allocates memory for 50 characters on the system stack during compile time. What if you don't know the size of array in advance? Here comes dynamic memory allocation of the array on heap during runtime. C provides following functions for dynamic memory management. These functions are defined in < stdlib.h >.



Dynamically allocating One Dimensional Array

          Following program illustrates the use of calloc function to allocate memory for 1 dimensional array and free function to release that memory. It is very important to use free otherwise there will be memory leak i.e allocated memory won't be deallocated and the memory will be held up without any use.

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------

Output :

Array elements : 10 20 30 40 50

----------------------------------------------------------------------------------------------------------------------------

           In the above program, we can also use malloc instead of calloc to allocate memory for an array of size n. We will demonstrate the use of malloc function in next program.

Dynamically allocating Two Dimensional Array

           Following program illustrates how memory for a two dimensional array is dynamically allocated and freed using calloc and free function. We can use malloc and calloc interchangeably. The only advantage of calloc is that the memory is initialized after allocation. 

----------------------------------------------------------------------------------------------------------------------------





----------------------------------------------------------------------------------------------------------------------------



Re-allocating memory

          In the following program, we allocate memory using malloc to hold a string. Then, we resize the allocated memory using realloc function to hold a larger string. Internally, realloc allocates a new block of memory, copies the contents of old block and frees up the old block of memory.

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------

Output :



----------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------




----------------------------------------------------------------------------------------------------------------------------

Output :



----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment