Created this really bad program
#include <stdio.h>
#include <ctype.h>
// forward declarations
void bad_function()
{
int *test = 5;
free(test);
}
int main(int argc, char *argv[])
{
bad_function();
return 0;
}
gcc -g ex1.c
./a.out //or whatever gcc creates
Segmentation fault (core dumped)
gdb -c core a.out
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault. #0 __GI___libc_free (mem=0x5) at malloc.c:2929 2929 malloc.c: No such file or directory.
(gdb) where
#0 __GI___libc_free (mem=0x5) at malloc.c:2929 #1 0x0000000000400549 in bad_function () at ex1.c:12 #2 0x0000000000400564 in main (argc=1, argv=0x7fffb825bd68) at ex1.c:19
Since I compiled with -g you can see that calling where tells me that it didn't like the code on line 12 of bad_function()
Then I can examine the test variable that I tried to free
(gdb) up
#1 0x0000000000400549 in bad_function () at ex1.c:12 12 free(test);
(gdb) print test
$1 = (int *) 0x5
(gdb) print *test
Cannot access memory at address 0x5
In this case the bug is pretty obvious I tried to free a pointer that was just assigned the address 5 which wasn't created by malloc so free has no idea what to do with it.