#include <stdio.h>
#include <stdlib.h>
int main(){
int * ptr = (int*)malloc(sizeof(int)*100); // Allocated space for 100 integers
// Some code
free(ptr); // Calling free with ptr as argument
return 0;
}
How does this free all 400 bytes (in my case)?
ptr
only contains address of one byte in the memory and also I have not passed any other argument specifying the size of the dynamic array so that it may run a loop and frees all the bytesWhat will happen if I do this:
ptr++; free(ptr);
Since we cannot retrieve the size of the array in heap by giving the pointer then it means
malloc()
has no clue how many bytes were reserved along withptr
then why does it not allocate another heap memory starting from the middle of previous array?