Note: sbrk and brk are used to allocate memory.
These are deprecated (Nowadays mmap is preferred):
These were deprecated in POSIX 1997, and removed in POSIX.1-2001. That being said, most UNIXes still have them (Linux and Mac, for legacy compatibility reasons).
Here's a possible implementation of sbrk.
static uintptr_t current_break; // Some global variable for your application.
// This would probably be properly tracked by the OS for the process
void *sbrk(intptr_t incr)
{
uintptr_t old_break = current_break;
+= incr;
current_break return old_break;
}
And let's use it (note we're using the OS' sbrk
, defined
in <unistd.h>
):
#include <stdio.h>
#include <unistd.h>
int main(void) {
void *curr_address = sbrk(0);
("current heap location: %p\n", curr_address); // our program break
printf
(16); // sbrk returns the old break, so this is still where curr_address was.
sbrkvoid *next_address = sbrk(0);
("current heap location: %p\n", next_address); // curr_address + 16
printf
(-16); // sbrk returns the old break, so this is still where curr_address was.
sbrkvoid *new_address = sbrk(0);
("current heap location: %p\n", new_address); // back to curr_address
printf}
Here's a possible implementation of brk.
static uintptr_t current_break;
int brk(void* end_data_segment) {
= end_data_segment;
current_break return 0;
}
To set a pointer:
#include <unistd.h>
int main(void) {
(0); // set current data segment to 0.
brk}
Since a lot of libc functions end up using sbrk
and
brk
under the hood, be careful with adjusting the heap with
sbrk
and brk
. It might've readjusted or you
might've readjusted the pointer for other calls.