Tuesday, October 23, 2018

C Language Memory Allocation Functions for B.C.A., B.Sc.(IT) and P.G.D.C.A. Students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOT- IT)
Contact No : 7600044051
Email : rupareleducation@gmail.com


Dynamic memory allocation in C

The 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.
  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

  • malloc() : allocates single block of requested memory.
  • calloc()  : allocates multiple block of requested memory.
  • realloc() : reallocates the memory occupied by malloc() or calloc() functions.
  • free()  : frees the dynamically allocated memory

malloc() :

  • The 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:

ptr=(cast-type*)malloc(byte-size) 


calloc() :

  • The 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:

ptr=(cast-type*)calloc(number, byte-size)


realloc() :

  • If 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.

ptr=realloc(ptr, new-size) 


free() :

  • The 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.


free(ptr)  


:: Best Of Luck ::

Please comment and share it..... Thank You.