malloc():  is the standard library function available in stdlib.h header file. it is used to  allocate memory dynamically from heap during program execution.

The malloc() in C Programming Language
In this section we will discuss dynamic memory allocation using malloc in C programming language. Malloc() is a predefined library function for memory allocation. Malloc() is used to allocate a specified size of memory block at the time of program execution. This means it makes dynamic memory allocation at run time when the programmer don’t know how much memory space the program needs.
As per the need we can change the size of allocated memory space at any time during program execution. It uses a pointer to locate the memory address and the default value of this pointer is void. To define the size malloc() uses the sizeof() function. sizeof() is a predefined C library function that returns the size of a variable.
Let Study malloc() function step by step in Detail.
malloc(): is the standard library function available in <stdlib.h> header file. Malloc() function is used to allocate memory dynamically from heap during program execution.
Here's is a step by step explanation how malloc() works in C program:

1.Function prototype:
Function prototypes is the declaration of a function that declares and define the type of value passed to the program and returned from the function.
The prototype for malloc() function is declared in the <stdlib.h> header file in C Language.

The Syntax : void *malloc(size_t size);

void *: The malloc() function returns a void pointer means it can implicitly cast to any pointer type in c program. Size_t size: is the only single argument to this function , size specifies the total number of bytes to allocate.
malloc() returns a pointer to the allocated memory. If malloc() fails to allocate memory it returns NULL.


2.Memory Allocation: When you call malloc(), it requests a block of memory of the specified size from the heap, which is a region of memory set aside for dynamic memory allocation during program execution. When the Malloc( ) function is called , the function request area of memory or a block of memory of the size specified. an area of memory or block of memory is allocated dynamically during program execution.
3.Dynamic Memory: dynamic memory allocation in c allows us to allocate memory during program run time or execution instead of compile time. Dynamic allocation is essentially useful when we do not know how much memory needs at compile time.

4. Returns a Pointer: malloc() function returns a void pointer to the allocated memory block. The type of this pointer is of void *, so we can explicitly convert or cast it to any pointer type before using it in the program. For example:

float *ptr = (float *)malloc(20 * sizeof(float));

5. Error Handling: in some situations the malloc() function fails to allocate memory space if there is not sufficient memory available on the heap area. In such scenarios , the returns a null pointer (NULL). It is very important to check the return value to handle such cases gracefully.
Let understand the example:

float *ptr = (int *)malloc(20 * sizeof(float));
if (ptr == NULL)
 {
  // executes the code here when allocation fails
 }


6. Memory Alignment:
The memory specified by this Malloc() function is properly allocated to any type of variable. This means we can use it for any data type found in a C program.


7. Memory Leak:
A common problem of the malloc() function is that it does not free the allocated memory when it is not useful . This concepts leads to memory leaks, where memory is allocated but never released. To free the memory allocated by malloc(), use the standard library free() function. consider the example:

float *ptr = (float *)malloc(20 * sizeof(float));
free(ptr); // Free the memory


C Program using malloc() function in c, to create a dynamic memory allocation for float array elements.

#include <stdio.h>
#include <stlib.h>
int main()
 {
  float* fptr;
  // allocate memory dynamically using malloc()
  fptr = (float*) malloc (6 * sizeof(float));
  if(fptr==NULL)
  {
   printf("OOPS! No space available try another day. \n");
   }
   else{
   printf("Memory has been allocated successfully!. \n");
   
   for(int i=0;i<6;i++)
   {
    fptr[i] = i*.23
   }
  //display the array elements
  printf("The array Values are: \n");
  for(i=0;i<6;i++)
  {
    printf("\n%.2f",fptr[i]);
  }
  }
 free(fptr);
 }

OUTPUT
Memory has been allocated successfully!.
The array Values are:
0.00
0.23
0.46
0.69
0.92
1.15


Advantages of malloc() in C
1. malloc() function allows us to allocate memory at run time .
2. The size of allocated memory can be change at any point in the program using malloc().
3. The pointer points to the first element and works like a array in C.
Disadvantages of malloc() in C Programming
1. Malloc() is not used in embedded system..
2. We need to remember the memory size or space while using the malloc() function for allocating memory dynamically.



Previous Topic:-->> Other directives in C. || Next topic:-->>calloc() in C.