Skip to content

Instantly share code, notes, and snippets.

@jamesgolick
Last active December 17, 2015 10:09
Show Gist options
  • Save jamesgolick/5593158 to your computer and use it in GitHub Desktop.
Save jamesgolick/5593158 to your computer and use it in GitHub Desktop.
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);
}
struct malloc_chunk *left = (struct malloc_chunk *)(ptr - footer_left(ptr) - sizeof(size_t));
struct malloc_chunk *right = (struct malloc_chunk *)(ptr + header(ptr)->size);
@dhamidi
Copy link

dhamidi commented May 16, 2013

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 from ptr or use (char*)ptr instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment