Difference between brk and sbrk
int brk(void *addr);
brk() sets the end of the data segment to the value specified by addr, when that value is reasonable, the system has enough memory, and the process does not exceed its maximum data size.
On success, brk() returns zero. On error, -1 is returned, and errno is set to ENOMEM.
void *sbrk(intptr_t increment);
sbrk() increments the program's data space by increment bytes. Calling sbrk() with an increment of 0 can be used to find the current location of the program break.
On success, sbrk() returns the previous program break. (If the break was increased, then this value is a pointer to the start of the newly allocated memory). On error, (void *) -1 is returned, and errno is set to ENOMEM.
brk
sets the upper limit of the data segment, sbrk
increments it. In ancient Unixes malloc/free
used sbrk
. On modern ones things could be very different, for example, OSX does not use brk/sbrk
to manage heap allocations but mmap
, brk/sbrk
exist but are just emulation in a small segment of memory. This is almost the same on Linux (source code of mention the history of transition from brk/sbrk to mmap).