Table of contents
1.
Introduction
1.1.
Dynamic Memory Allocation
1.2.
Free() Method
2.
Marking Mechanism to free memory in C/C++
3.
How does free() know the size of memory to be deallocated?
3.1.
The only parameter passed to free() is a pointer, not a size. So the issue is, how does the free() function know how big the block to deallocate is?
4.
Frequently Asked Questions
4.1.
What do you mean by Memory Leak?
4.2.
When should we use the register storage specifier?
4.3.
Can a C program be compiled or executed in the absence of a main()?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

How does the free() method know the size of memory to be de-allocated?

Author Akash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The C programming language, the first of its kind, is a procedural programming language. C is also widely used in low-level system programming, embedded systems, and hardware development. It's also been greatly optimised over the years and is still used to create complex software like the FreeBSD operating system and the XNU kernel. Low-level memory access, a small set of keywords, and a clean style all contribute to the C language's suitability for system programming, such as operating system or compiler development. 

Moving forward, in this blog we will learn about Dynamic Allocation, and deallocation using the free() method in C language.

Also see : C Static Function and Short int in C Programming

Dynamic Memory Allocation

Dynamic Memory Allocation is a process that changes the size of a data structure (such as an Array) during execution.

C language has various functions to help with these tasks. To enable dynamic memory allocation in C programming, C provides library functions specified in the <stdlib.h> header file.

Free() Method

Now, let us have a brief overview of the free() method in C language. 

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);
You can also try this code with Online C Compiler
Run Code

You can also read about the memory hierarchy, and  Tribonacci Series

Marking Mechanism to free memory in C/C++

Internally, the free () function’s job is to mark the dynamically allocated memory section as “false” or 0.

When we allocate memory of a particular size using the malloc() function, it returns the address of allocated memory and marks that section as true or 1 in one of the table/structure.

So, function free (), just need to know the address and not size of the dynamically allocated memory. Marking memory as false or 0 is known as de-allocation of memory.

How does free() know the size of memory to be deallocated?

The only parameter passed to free() is a pointer, not a size. So the issue is, how does the free() function know how big the block to deallocate is?

The heap area allocated after the dynamic memory allocation is one word larger than the requested memory. The additional word is used to store the allocation size and is later utilized by free ( ). So, this is how the free() method knows about the size of the block to be deallocated.

You can also read about the dynamic arrays in c.

Frequently Asked Questions

What do you mean by Memory Leak?

Memory leaks occur when a programmer allocates dynamic memory to a programme but fails to free or remove the utilised memory when the code is completed. This is dangerous if the software includes daemons and servers.

When should we use the register storage specifier?

If a variable is utilised frequently, we use Register Storage Specifier. Because the variable will be defined in one of the CPU registers, this aids the compiler in locating it.

Can a C program be compiled or executed in the absence of a main()?

Although the code will be compiled, it will not be run. main() is necessary to run any C programme.

Conclusion

In this article, we have extensively discussed the memory de-allocation of the free() method used in the C language. We hope this blog has helped you enhance your knowledge regarding the free() 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(), and Preprocessors in C. 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!

 

Live masterclass