Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

How to deallocate memory without using free method in C Language?

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

memory allocation and deallocation are one of the most important aspects of space management in a programing language . In this article, we would be discussing the deallocation of memory without using free() method. But before getting into alternatives to free() let's see what is free() method. 

The "free" method in C is used to deallocate memory dynamically. The memory allocated by malloc() and calloc() is not automatically deallocated. As a result, the free() function is utilized anytime dynamic memory allocation occurs. It frees up memory, which helps to prevent memory waste.

Syntax

free(ptr);

Also See, Sum of Digits in C and C Static Function.

Realloc() Method

To deallocate previously allocated memory, we will use the standard library function realloc(). The "realloc()" function declaration from "stdlib.h" is shown below.

Syntax: 

void *realloc(void *ptr, size_t size);

 

The above statement creates a new memory space with the size defined in the variable newsize. The pointer will be returned to the first byte of the memory block when the function has been executed. The new memory size might be more or lower than the old memory size. We don't know if the newly allocated memory block will point to the same place as the prior memory block. In C, the realloc function will replicate all old data into the new area. It ensures that data is kept secure.

Note: The call to realloc is equal to "free(ptr)" if "size" is zero. If the pointer “ptr” is NULL and the size is non-zero, calling realloc is the same as doing malloc(size).

You can also read about the dynamic arrays in c and  Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Need for realloc() method

Realloc ( realloc() method ) returns a pointer to a new object with the size provided by size and deallocates the old object referenced to by ptr. Prior to deallocation, the contents of the new object are identical to those of the old object, up to the smaller of the new and old sizes. Any bytes in the new object that are larger than the old object's size have uncertain values.

For Example:

/* code with memory leak */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int *ptr = (int*)malloc(10);
    return 0;
}

 

Result: On Checking the memory leak with the Valgrind tool, 10 bytes of memory are leaked, highlighted in red.

 [akash@ubuntu]$ valgrind –leak-check=full ./free
  ==1238== LEAK SUMMARY:
  ==1238== definitely lost: 10 bytes in 1 blocks. //Memory Leak
  ==1238== possibly lost: 0 bytes in 0 blocks.
  ==1238== still reachable: 0 bytes in 0 blocks.
  ==1238== suppressed: 0 bytes in 0 blocks.
[akash@ubuntu]$

 

Now, we will be modifying the above code using the realloc() method.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int *ptr = (int*) malloc(10);
    //realloc is being called with size = 0
    realloc(ptr, 0);
    return 0;
}

 

Result: Examine the Valgrind output. No memory leaks are conceivable, as shown by the red hue.

  [akash@ubuntu]$ valgrind –leak-check=full ./a.out
  ==1435== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
  ==1435== malloc/free: in use at exit: 0 bytes in 0 blocks.
  ==1435== malloc/free: 1 allocs, 1 frees, 10 bytes allocated.
  ==1435== For counts of detected errors, rerun with: -v
  ==1435== All heap blocks were freed — no leaks are possible. // No leak
  [akash@ubuntu]$

Frequently Asked Questions

What are the uses of realloc () and free () methods?

The main function of realloc() is that it is used to change the size of previously allocated space. On the other hand, the free() method deallocates the previously allocated space.

Is it better to use malloc () or calloc ()?

The calloc method sets the allocated memory to 0, whereas malloc does not. As a result, the junk data is stored in the memory allocated by malloc. In other words, calloc is the result of combining malloc with memeset.

Conclusion

In this article, we have extensively discussed the memory de-allocation without using the free() method in C language.

We hope this blog has helped you enhance your knowledge regarding the realloc() method in C language. Some official documentation on big data that can help you improve your understanding is C #ifndef and Dynamic Memory Allocation.

If you would like to learn more, check out our articles on realloc()Basics Of C Languagemalloc(), Preprocessors in C, and  Tribonacci Series.

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Topics covered
1.
Introduction
2.
Realloc() Method
3.
Need for realloc() method
4.
Frequently Asked Questions
4.1.
What are the uses of realloc () and free () methods?
4.2.
Is it better to use malloc () or calloc ()?
5.
Conclusion