Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Dynamic memory allocation

Some time is better to allocate size dynamically that is at Run-time. It avoids two types of problems:
1) At the run time if previously allocated memory is insufficient, we can increase memory size at runtime. Therefore it avoids memory insufficiency.
2) At the run time if previously allocated memory is more than sufficient then it results in wastage of memory. With the help of dynamically memory allocation, we can reduce the memory size at run time. Therefore it avoids memory wastage.

Following are the functions that help the user to allocate memory dynamically (at runtime) according to need.

malloc()
calloc()
realloc()
free()

Now let’s study each function in detail.

 

The malloc() function

Syntax

void *malloc(size_in_bytes)

The malloc() is an abbreviation of memory allocation.
It allocates memory (in a heap) dynamically (at runtime) according to the users need.
If malloc() function fails to allocate specified size, it returns NULL.
Let’s see how size is declared & allocated in memory using the malloc() function.

Memory Allocation syntax

pointer_name = (casting_data_type*)malloc(size_in_bytes)

Example

C Programming Language Dynamic Memory Allocation using malloc() Function

Example

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int number,i;
    int *ptr;
    printf("\n Enter number of elements = ");
    scanf("%d",&number);
    ptr=(int*)malloc(number*sizeof(int));
    if(ptr==NULL)
    {
        printf("\n Insufficient memory");
        exit(0);
    }
    for(i=0;i<number;i++)
    {
        *(ptr+i) = i+1;
    }
    printf("\n Integers present in dynamically allocated memory are =");
    for(i=0;i<number;i++)
    {
        printf(" %d",*(ptr+i));
    }
    return 0;
}

Output

Enter number of elements = 7

Integers present in dynamically allocated memory are = 1 2 3 4 5 6 7

 

The calloc() function

It is an abbreviation of contiguous allocation.
There are few differences between malloc() and calloc() functions.

Differences are as given below:

1) The malloc() takes one argument, and calloc() takes two arguments.
2) The calloc() function works little slower than malloc() function.
3) After declaration, malloc() function initializes allocated memory with garbage value whereas calloc() function initializes the memory with zero.

Syntax

void *calloc(element_number, size)

Memory Allocation syntax

pointer_name = (casting_data_type*)calloc(element_number, size)

 

C Programming Language Dynamic Memory Allocation using calloc() Function

Example

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int number,i;
    int *ptr;
    printf("\n Enter number of elements = ");
    scanf("%d",&number);
    ptr=(int*)calloc(number,sizeof(int));
    if(ptr==NULL)
    {
        printf("\n Insufficient memory");
        exit(0);
    }
    for(i=0;i<number;i++)
    {
        *(ptr+i) = i+1;
    }
    printf("\n Integers present in dynamically allocated memory are =");
    for(i=0;i<number;i++)
    {
        printf(" %d",*(ptr+i));
    }
    return 0;
}

Output

Enter number of elements = 9

Integers present in dynamically allocated memory are = 1 2 3 4 5 6 7 8 9

 

The realloc() function

It is an abbreviation of reallocation.
It is used to reallocate (change) the memory size allocated by malloc() or calloc().

Syntax

void *realloc(void *pointer_name, new_size);

Memory Allocation syntax

pointer_name = (casting_data_type*)realloc(pointer_name, new_size);

Example no 1

C Programming Language Dynamic Memory Allocation using realloc() Function 1Example no 2

C Programming Language Dynamic Memory Allocation using realloc() Function 2

 

The free() function

In heap, memory allocated by malloc(), calloc() or realloc() is not destroyed/freed automatically.
We need to make it free explicitly.

Syntax

void free(pointer_name);

Example
C Programming Language free() Function Working

 

Leave a reply