Dynamic Memory Allocation :

>>An statically allocated variable or array has a fixed size in memory . 

>>Dynamic Memory Allocation is a way in which the size of a data structure can be changed during runtime

>>Memory assigned to a program in a typical architecture can be broken down into four segments :

    * Code 

    * Static/Global variable 

    * Stack

    * Heap

 

>> In Dynamic memory allocation , the memory is allocated at runtime from the heap segment 

>> We have four function that help us achieve this task:

    * malloc

    * calloc

    * realloc

    * free

 

>> Function : malloc()

   . malloc() stands for memory allocation

   . It reserves a block of memory with the given amount of bytes .

   . Return value is a void pointer to the allocated space

   . Therefore the void pointer need to casted to the appropriate type as per the requirements 

   . However, if the space is insufficient , allocation of memory fails and it returns a Null pointer. 

   . All the values at allocated memory are initialized to garbage values 

    syntax :

    ptr = (ptr-type*) malloc(size_in_bytes);   

Comments