Skip to content

Instantly share code, notes, and snippets.

@stefafafan
Created October 12, 2013 04:00
Show Gist options
  • Save stefafafan/6945663 to your computer and use it in GitHub Desktop.
Save stefafafan/6945663 to your computer and use it in GitHub Desktop.
realloc implementation
void *realloc(void *ptr, size_t size)
{
char *p;
char *a = ptr;
// Realloc on a NULL pointer same as malloc.
if (ptr == NULL)
{
p = malloc(size);
return p;
}
// Realloc to size 0 same as free.
else if (size == 0)
{
free(ptr);
return NULL;
}
// Cannot realloc to size smaller.
else if(size > *(a - 8)){
return NULL;
}
// Malloc and copy to given pointer, then free the temporary portion.
else
{
p = malloc(size);
bcopy(ptr, p, size);
free(p);
return p;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment