Created
October 12, 2013 04:00
-
-
Save stefafafan/6945663 to your computer and use it in GitHub Desktop.
realloc implementation
This file contains hidden or 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
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