I have this simple test C program which leaks 4 bytes of memory:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* x = malloc(sizeof(int));
printf( "Address: %p\n", x);
return 0;
}
I compile it with gcc -o leak leak.c
, and then run it:
$ leak
Address: 0x55eb2269a260
Then I create another test C program that will try to free the leaked memory:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
void *addr = (void*)0x55eb2269a260;
printf( "Trying to free address: %p\n", addr);
free(addr);
return 0;
}
I compile it with gcc -o myfree free.c
and then run it:
$ myfree
Trying to free address: 0x55eb2269a260
Segmentation fault (core dumped)
What is happening here? Why is it not possible to free the leaked memory?