Dynamic Memory Allocation in C using malloc() Function


In C programming, the malloc() function is used to dynamically allocate memory during runtime. It is defined in the stdlib.h library and is used to allocate a block of memory of a specified size. The size of the memory block is specified in bytes.
The malloc() function returns a pointer to the first byte of the allocated memory block. The memory allocated by malloc() is not initialized, so it contains garbage values.
      Syntax :
             void* malloc ( size_t size)
      Example :
             int *ptr = int* malloc ( 5 * size ( int ) )
             int *ptr = int* malloc ( 5 * 4 )
             int *ptr = int* malloc ( 20 )

  • Here is an example of how the malloc() function can be used in C:
  • The program demonstrates the use of the malloc() function to dynamically allocate memory for an array of integers during runtime.
  • The program starts by prompting the user to enter the limit of the array. The malloc() function is then used to allocate memory for the array. The amount of memory allocated is calculated by multiplying the size of an integer by the number of elements in the array (n). The malloc() function returns a pointer to the first byte of the allocated memory block and the pointer is type casted to int*.
  • The program then checks if the memory allocation was successful by checking if the pointer returned by the malloc() function is null. If the pointer is null, the program prints a message indicating that the memory allocation failed and exits with a status code of 1.
  • Otherwise, the program prompts the user to enter integers for each element of the array. The integers are then stored in the memory block that was allocated by the malloc() function. The program then prints the integers that were entered by the user.
  • Finally, the program releases the memory that was allocated by the malloc() function by using the free() function. This is an important step in memory management and should always be done when memory is dynamically allocated to avoid memory leaks.

Source Code

//malloc in Pointers
#include<stdio.h>
#include<stdlib.h>
int main()
{
    //void* malloc(size_t size)
    int i,n;
    printf("\nEnter The Limit : ");
    scanf("%d",&n);
 
    int *ptr=(int *)malloc(n*sizeof(int));
 
    if(ptr==NULL)
    {
          printf("Memory Not Available ...");
          exit(1);
    }
    for(i=0; i<n; i++)
    {
        printf("Enter a integer : ");
        scanf("%d",ptr+i);
    }
    for(i=0;i<n;i++)
    {
        printf("%d  ",*(ptr+i));  // ptr+1
    }
 
    return 0;
}
 
 
 
To download raw file Click Here

Output

Enter The Limit : 5
Enter a integer : 1
Enter a integer : 2
Enter a integer : 3
Enter a integer : 4
Enter a integer : 5
1  2  3  4  5

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C