Last active
December 17, 2015 10:09
-
-
Save jamesgolick/5593158 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct malloc_chunk { | |
struct malloc_chunk *next; // next pointer on free list | |
size_t size; // the size of this chunk | |
unsigned free; // > 0 if this pointer is free - useful for runtime assertions | |
} | |
struct malloc_chunk * | |
header(void *ptr) | |
{ | |
return (struct malloc_chunk *)ptr - sizeof(malloc_chunk); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct malloc_chunk *left = (struct malloc_chunk *)(ptr - footer_left(ptr) - sizeof(size_t)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct malloc_chunk *right = (struct malloc_chunk *)(ptr + header(ptr)->size); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe the address calculation in line 10 of header.c is incorrect. Your current calculation gets you
ptr
-sizeof(struct malloc_chunk) * sizeof(struct malloc_chunk)
bytes. Either subtract one fromptr
or use(char*)ptr
instead.