What is Malloc() function in C Programming Language?



What is Malloc() function in C Programming Language?

A malloc() function is used for  dynamic memory allocation. It is defined in stdlib.h or malloc.h depending on the operating system. Memory allocation by malloc() is persistent.  
  • void  *malloc(size_t size);


Example:

  • #include<stdio.h>
  • #include<stdlib.h>
  • void main()
  • {
  •           int array[10];
  •           int *ptr = (int *) malloc(10 * sizeof (int));
  • if (ptr == NULL)
  • {
  • }
  •           else
  •          {
  •                   free(ptr);
  •                   ptr=NULL;
  •           }
  • }

No comments:

Post a Comment