I have a question regarding the free(ptr) function and how it deallocates memory. From my understanding, malloc, calloc, and realloc are used to dynamically allocate memory, and free(ptr) is used to release that memory once it is no longer needed. I am interested in understanding how free(ptr) manages to deallocate the memory, However, I would appreciate a more detailed explanation of the actual process behind free(ptr)
C:
#include<stdio.h>
#include<stdlib.h>
int main()
{
// Allocate memory for an integer array of size 5
int *ptr = malloc(5 * sizeof(ptr));
// Check if memory allocation was successful
if (ptr != NULL)
{
// Assign values to the elements of the allocated array
ptr[0] = 10;
ptr[1] = 20;
ptr[2] = 30;
ptr[3] = 40;
ptr[4] = 50;
}
// Free the dynamically allocated memory
free(ptr);
// Return 0 to indicate successful execution
return 0;
}