After allocating a single large block of memory with the specified size using malloc function, then you use the free() function to deallocate that single large block of memory.
There is this one confusion. See code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr; //This pointer will hold the base address of the block created
int n, i;
if(ptr == NULL)
{
printf("Memory not allocated\n");
}
printf("Enter the number of elements for the array: ");
scanf("%i", &n);
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
// Check if the memory has been successfully allocated by malloc or not
if(ptr == NULL)
{
printf("Memory not allocated\n");
exit(0);
}
else
{
printf("\nMemory successfully allocated using malloc\n");
printf("Value of *ptr: %i\n", *ptr);
printf("Address of ptr: %p\n", ptr);
printf("Size of *ptr: %li\n\n", sizeof(*ptr));
// Get the elements of the array
printf("Initial elements in ptr: \n");
for(i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
printf("\n\n");
// Get the addresses of each of the elements in the array
printf("Addresses of each elements in ptr: \n");
for(i = 0; i < n; ++i)
printf("%p, ", &ptr[i]);
printf("\n\nInitializing some values for the first few arrays:\n");
ptr[0] = 10;
ptr[1] = 100;
ptr[2] = 5000;
printf("Elements in ptr: \n");
for(i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
printf("\n\n");
printf("Addresses of each elements in ptr: \n");
for(i = 0; i < n; ++i)
printf("%p, ", &ptr[i]);
printf("\n\nReinitializing some values for the first few arrays:\n");
ptr[0] = 20;
printf("Elements in ptr: \n");
for(i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
printf("\n\n");
printf("Addresses of each elements in ptr: \n");
for(i = 0; i < n; ++i)
printf("%p, ", &ptr[i]);
printf("\n\n");
printf("Size of each Element in ptr: \n");
for(i = 0; i < n; ++i)
printf("%li, ",sizeof(ptr[i]));
free(ptr);
printf("\n\nFreeing ptr: \n");
printf("Size of each Element in ptr: \n");
for(i = 0; i < n; ++i)
printf("%li, ",sizeof(ptr[i]));
printf("\n\n");
printf("Elements in ptr: \n");
for(i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
printf("\n\nSize of *ptr: %li\n\n", sizeof(*ptr));
}
return 0;
}
The output is as follows:
Code:
Enter the number of elements for the array: 5
Memory successfully allocated using malloc
Value of *ptr: 0
Address of ptr: 0x55e7db3c0ac0
Size of *ptr: 4
Initial elements in ptr:
0, 0, 0, 0, 0,
Addresses of each elements in ptr:
0x55e7db3c0ac0, 0x55e7db3c0ac4, 0x55e7db3c0ac8, 0x55e7db3c0acc, 0x55e7db3c0ad0,
Initializing some values for the first few arrays:
Elements in ptr:
10, 100, 5000, 0, 0,
Addresses of each elements in ptr:
0x55e7db3c0ac0, 0x55e7db3c0ac4, 0x55e7db3c0ac8, 0x55e7db3c0acc, 0x55e7db3c0ad0,
Reinitializing some values for the first few arrays:
Elements in ptr:
20, 100, 5000, 0, 0,
Addresses of each elements in ptr:
0x55e7db3c0ac0, 0x55e7db3c0ac4, 0x55e7db3c0ac8, 0x55e7db3c0acc, 0x55e7db3c0ad0,
Size of each Element in ptr:
4, 4, 4, 4, 4,
Freeing ptr:
Size of each Element in ptr:
4, 4, 4, 4, 4,
Elements in ptr:
0, 0, -616824816, 21991, 0,
Size of *ptr: 4
When using the free() function, it clears the values within ptr there are values such as -616824816, 21991 instead of all 0. I am confused as to how free() works? How does de-allocating memory work in C?