C-dynamic-memory-allocation .

In this session i have discussed dynamic memory allocation in C .


The array is collection of similar data type at contiguous memory location . The size of array is fixed . So it is sometimes sufficient and sometimes more than required . Dynamic memory allocation allows a program to get more memory space , while running or to release space when no space is required.

We can achieve the dynamic memory allocation with 4 library function under stdlib.h header file . These functions are as follows .

1-malloc()

1-calloc()

1-realloc()

1-free()


1-malloc()

The malloc stand for memory allocation . It is reserves the block of memory of specified size and returns pointer of type void which can be casted into pointer of any form .

Syntax

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

Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.

ptr=(int*)malloc(100*sizeof(int));

This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.

Example : Write a program that demonstrate the malloc function .

    #includ<stdio.h> 
    #include<stdlib.h>
    int main()
    {
        int n,i,*ptr,sum=0;
        printf("Enter number of elements: ");
        scanf("%d",&n);
        //memory allocated using malloc
        ptr=(int*)malloc(n*sizeof(int));  
        if(ptr==NULL)                     
        {
            printf("Error! memory not allocated.");
            exit(0);
        }
        for(i=0;i<n;++i)
        {
            printf("Enter the element");
            scanf("%d",ptr+i);
        }
        printf("Entered element in array");
        for(i=0;i<n;++i)
        {
            printf("%d",*(ptr+i));
        }
        free(ptr);
        return 0;
    }
OUTPUT : Enter the number of element 5
         Enter the element 2
         Enter the element 4
         Enter the element 5
         Enter the element 3
         Enter the element 10
         Entered element in array
         2 4 5 3 10
    

2-calloc()

The name calloc stands for "contiguous allocation". The calloc function allocate the multiple block of memory with same size and set all bytes to zero . The difference between malloc and calloc is that malloc allocate the single block of memory and calloc allocate the multiple block of memory .

Syntax

ptr=(cast-type*)calloc(n,element-size);

ptr=(int*)calloc(25,sizeof(int));

Here , It create the contiguous memory block for 25 element with same size 2 bytes or 4 bytes it depends on 32 bit or 64 bit .

Example : Write a program that demonstrate the calloc function .

    #includ<stdio.h> 
    #include<stdlib.h>
    int main()
    {
        int n,i,*ptr,sum=0;
        printf("Enter number of elements: ");
        scanf("%d",&n);
        //memory allocated using malloc
        ptr=(int*)malloc(n*sizeof(int));  
        if(ptr==NULL)                     
        {
            printf("Error! memory not allocated.");
            exit(0);
        }
        for(i=0;i<n;++i)
        {
            printf("Enter the element");
            scanf("%d",ptr+i);
        }
        printf("Entered element in array :\n");
        for(i=0;i<n;++i)
        {
            printf("%d",*(ptr+i));
        }
        free(ptr);
        return 0;
    }
    
 OUTPUT : Enter the number of element 5
          Enter the element 2
          Enter the element 4
          Enter the element 5
          Enter the element 3
          Enter the element 10
          Entered element in array
          2 4 5 3 10
    

3-realloc()

The realloc function is used to change the size of previously allocated memory .

Syntax

ptr=realloc(ptr,newsize);

Example : Write a program check the userid and password for login .

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        int *ptr,i,n1,n2;
        printf("Enter size of array: ");
        scanf("%d",&n1);
        ptr=(int*)malloc(n1*sizeof(int));
        printf("Address of previously allocated memory: ");
        for(i=0;i<n1;++i)
        {
            printf("%u\t",ptr+i);
        }
        printf("\nEnter new size of array: ");
        scanf("%d",&n2);
        ptr=realloc(ptr,n2);
        for(i=0;i<n2;++i)
        {
            printf("%u\t",ptr+i);
        }
        return 0;
    }
OUTPUT : Enter size of array : 3
         Address of previously allocated memory 4984280 4984284 4984288
         Enter new size of array 4
         4984280 4984284 4984288 4984292
    

4-free()

The free function is used to free allocated memory . In above all example we have used the free() please see .


Welcome to AndroidATPoint.

back image                     next image