Dynamic Memory Allocation in C

July 25, 2021 0 Comments

Dynamic Memory Allocation in C

Dynamic Memory Allocation in C, Dynamic memory allocation refers to memory allocation that occurs during the execution of a program and is controlled by the program.

The compiler has no information about such memory at compile time. It’s given to programs as and when they’re needed, and it can be deallocated as needed.

Dynamic variables are variables that can be assigned space as needed during the execution of a program or procedure, but can also be disposed away of and their space returned to the system by the program.

Dynamic memory allocation routines:

malloc:

The malloc() function allocates size bytes and returns a pointer to the allocated memory. the initial state of memory allocated by malloc is garbage state.

#include <stdlib.h>

void *malloc(size_t size);
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *ptr = NULL, num=0, i=0;
    printf("Enter the number \n");
    scanf("%d", &num);
    /*malloc example*/
    ptr = (int *)malloc(num * sizeof(int));
    
    if (ptr == NULL){
        printf("malloc failed\n");
        return -1;
    }
    
    for(i = 0; i < num; i++){
        ptr[i] = i+1;
    }
    
    for(i=0; i<num; i++){
        printf("%d ", ptr[i]);
    }
    printf("\n");
    free(ptr);

    return 0;
}

calloc:

The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero.

#include <stdlib.h>

void *calloc(size_t nmemb, size_t size);
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int *ptr = NULL, i = 0, num =0;
    printf("Enter the num\n");
    scanf("%d", &num);
    /*calloc example*/
    ptr = (int*)calloc(num, sizeof(int));

    if (ptr == NULL){
        printf("calloc failed\n");
        return -1;
    }
    for(i = 0; i < num; i++){
        ptr[i] = i+2;
    }
    
    for(i=0; i<num; i++){
        printf("%d ", ptr[i]);
    }
    printf("\n");
    free(ptr);
    return 0;
}

realloc:

The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized.

#include <stdlib.h>

void *realloc(void *ptr, size_t size);
#include <stdio.h>
#include<stdlib.h>

int main()
{
    int *ptr = NULL, i = 0, num = 0, num1 = 0;
    
    printf("Enter the num\n");
    scanf("%d", &num);
    ptr = (int*)calloc(num, sizeof(int));

    if (ptr == NULL){
        printf("calloc failed\n");
        return -1;
    }
    for(i = 0; i < num; i++){
        ptr[i] = i+2;
    }
    
    /*realloc example*/
    printf("Enter new size \n");
    scanf("%d", &num1);
    ptr  = (int *)realloc(ptr, num1*sizeof(int));
    if(ptr == NULL){
        printf("realloc failed\n");
        return -1;
    }
    for(i = num; i < num1; i++){
        ptr[i] = i+2;
    }
    
    for(i=0; i<num1; i++){
        printf("%d ", ptr[i]);
    }
    printf("\n");
    
    free(ptr);


    return 0;
}

free:

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc(), or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

#include <stdlib.h>

void free(void *ptr);

Share This: