Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Dynamic memory allocation in C language

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 118
    Comment on it

    Dynamic memory allocation allows the C programmer to allocate memory at runtime. Dynamic memory allocation in C is possible by 3 functions of stdlib.h header file.

    malloc()
    calloc()
    free()

    malloc() function

    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)

    Example

    #include <stdio.h>  
    #include <stdlib.h>  
    void main(){  
        int n,i,*ptr,sum=0;  
        printf("Enter number of elements: ");  
        scanf("%d",&n);  
        ptr=(int*)malloc(n*sizeof(int));  
        if(ptr==NULL)                       
        {  
            printf("Can't allocate memory");  
            exit(0);  
        }  
        printf("Enter elements of array: ");  
        for(i=0;i<n;++i)  
        {  
            scanf("%d",ptr+i);  
            sum+=*(ptr+i);  
        }  
        printf("Sum=%d",sum);  
        free(ptr);  
    }  
    

    OUTPUT
    Enter elements of array: 3
    Enter elements of array: 10
    10
    10
    Sum=30

    calloc() function

    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)

    Example

    #include <stdio.h>  
    #include <stdlib.h>  
    void main(){  
        int n,i,*ptr,sum=0;  
        printf("Enter number of elements: ");  
        scanf("%d",&n);  
        ptr=(int*)calloc(n,sizeof(int));  //memory allocated using calloc  
        if(ptr==NULL)                       
        {  
            printf("Sorry! unable to allocate memory");  
            exit(0);  
        }  
        printf("Enter elements of array: ");  
        for(i=0;i<n;++i)  
        {  
            scanf("%d",ptr+i);  
            sum+=*(ptr+i);  
        }  
        printf("Sum=%d",sum);  
        free(ptr);  
    }  
    

    OUTPUT
    Enter elements of array: 3
    Enter elements of array: 10
    10
    10
    Sum=30

    free() function in C

    free() function is used to free the memory occupied by malloc() or calloc() function. Otherwise, it will consume memory till the time program exits.

    syntax of free() function:

    free(ptr)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: