realloc() and free() function in C Programming Language

Rumman Ansari   Software Engineer   2019-03-31   10315 Share
☰ Table of Contents

Table of Content:


C language inherently does not have any technique to allocate memory dynamically, there are 4 library functions under stdlib.h for dynamic memory allocation.

Function Use of Function
malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space
calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory
free() deallocate the previously allocated space
realloc() Change the size of previously allocated space

We already discuss about malloc() function in previous chapter.

We already discuss about calloc() function in previous chapter.

realloc() function

If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc().

realloc () function modifies the allocated memory size by malloc () and calloc () functions to new size.

If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc() function. In short, it changes the memory size.

Syntax

ptr = realloc(ptr, newsize);

Example

int *ptr;
ptr = (int*)malloc(50 * sizeof(int));
ptr = (int*)realloc(ptr,100); //allocated a new memory to variable x

free() function in C

The free( ) function returns the memory pointed to by ptr to the heap. This makes the memory available for future allocation.

The memory occupied by malloc() or calloc() functions must be released by calling free() function. Otherwise, it will consume memory until program exit.

Program; realloc() function and allocating address position

In this program, we are going to allcate some memory using calloc() function.

#include 
void free(void *ptr);

#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: \n");
    for(i = 0; i < n1; ++i)
         printf("%u\t",ptr + i);

    printf("\nEnter new size of array: \n");
    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: 5
Address of previously allocated memory:
39128384        39128388        39128392        39128396        39128400

Enter new size of array:
3
39128384        39128388        39128392 


Program: realloc() and free() function

In this program, we are going to allcate some memory using calloc() function.


 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main()
{
    char *ptr;
    /* memory is allocated dynamically */
    ptr = malloc( 20 * sizeof(char) );
    if( ptr == NULL )
    {
        printf("Couldn't able to allocate requested memory\n");
    }
    else
    {
       strcpy( ptr,"atnyla.com");
    }
    printf("Dynamically allocated memory content  : " \
           "%s\n", ptr );
    ptr=realloc(ptr,100*sizeof(char));
    
    if( ptr == NULL )
    {
        printf("Couldn't able to allocate requested memory\n");
    }
    else
    {
        strcpy( ptr,"space is extended upto " \
                               "100 characters");
    }
    printf("Resized memory : %s\n", ptr );
    free(ptr);
}

Output

Dynamically allocated memory content  : atnyla.com
Resized memory : space is extended upto 100 characters
Press any key to continue . . .


Program:

This program allocates room for the strings entered by the user and then frees the memory:


#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *string[100];
int i;
for(i=0; i<100; i++) {
	if((string[i] = malloc(128))==NULL) {
	printf ("Allocation Error \n");
	exit (1);
}
gets(string[i]);
}
/* now free the memory */
for(i=0; i<100; i++)
 free(string[i]);
return 0;
}