I get a segmentation fault (core dumped)
in the following peace of code (I'm implementing malloc()
, free()
and realloc()
):
void free(void* ptr)
{
void* curr = head;
void* before = NULL;
int isLegal = 0;
/*Line X*/printf("curr is %p and ptr is %p\n", curr, ptr);
if(curr == ptr)
{
printf("aaa");
}
else
{
printf("bbb");
}
/*Some more code that actually frees the pointer and not relevant here*/
}
Now, you'd assume that it'd print aaa
or bbb
, it just announces a segmentation fault right after performing the printf()
in line X. If I type "printf("a")" instead of the current printf()
it won't print 'a' at all. That is really weird.
It prints:
curr is 0x86be000 and ptr is 0x86be000
and yet it would just exit and throw a segmentation fault right after.
The variable head
is a static variable in that file. I really want to know where the problem is, it's really weird. Here's the statement from the header file:
void free(void* ptr);
As simple as that, do you see any problem in here? The full code is available here but I doubt it's related, the program should, at least, print either 'aaa' or 'bbb', and it doesn't do that. Any idea? I'm really desperate.
Following code complied with warnings but did execute perfectly
Output :
note - Instaed of your mm.h include I included codes in same file