I'm trying to implement malloc
on CentOS, but I keep getting the error:
malloc.c: In function ‘malloc’:
malloc.c:11:5: error: implicit declaration of function ‘sbrk’ [-Werror=implicit-function-declaration]
mem_ptr = sbrk(SIXTY_FOUR_K); /* Allocate 64 kB of memory */
Here is the code that the compiler warning is referencing:
#include "malloc.h"
#include <unistd.h>
void * malloc(size_t bytes) {
uintptr_t mem_ptr;
if (bytes <= 0) { /* If user passes in bad value, return NULL */
return NULL;
}
mem_ptr = sbrk(SIXTY_FOUR_K); /* Allocate 64 kB of memory */
if (mem_ptr == -1) { /* sbrk() failed */
return NULL;
}
return (void *)mem_ptr;
}
According to the documentation on sbrk
, you should just have to import unistd.h
, which I do. Is there something I'm doing wrong?
Did you take a look at the Feature Test Macro requirements?
See if compiling with something like
-D_SVID_SOURCE
will work (though it looks like there are a number of options based on that macro list)As of glibc 2.19, a new feature test macro was added,
_DEFAULT_SOURCE
which is meant to replace_BSD_SOURCE
and_SVID_SOURCE
. For more information on_DEFAULT_SOURCE
, see this question: What does -D_DEFAULT_SOURCE do?