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 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.