Header Ads Widget

Ticker

6/recent/ticker-posts

Dynamic memory allocation in c

 1) Allocation of the memory at the run time ( i.e. during the execution of the program ) is known as Dynamic memory allocation.

2) Advantage of this is that memory can be allocated any time during the execution of the program as per the requirement which is not possible in static memory allocation .

3) Unlike array it saves the wastage of memory because if allocated memory not required it may be released.

4) Dynamic memory allocation can be handle by standard library function of memory management which are given below:-



(i) Malloc():-  It is used to allocated requested bytes of memory during the program and return the void pointer .
     Syntax:-   
If the required size of memory is not available then it return NULL.
Example:-   (int *) malloc (10 * sizeof( int) )

(ii) Calloc():- It takes two argument first for number of blocks and second for size of each block and allocates the memory accordingly .It initialize the each bytes to zero and returns the void pointer to the first byte of the allocated memory . 
If the required size of memory is unavailable then it return NULL.
Syntax:-

(iii) free():-
                      It releases the already allocated memory.
     Syntax:-   free( Ptr_name )

(iv) realloc():- realloc function is use to resize the already allocated memory dynamically . It is used when we want to increase or reduce the size of already allocated memory.


👉Singly link list--Next>>

Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-



                       
                            
                

Post a Comment

0 Comments