brk(2)
๐ brk(2) โ bestie edition ๐
okay so this is a page from the System Calls Manual (section 2, iykyk) and we're gonna talk about brk and sbrk. no cap.
โจ NAME โจ
brk, sbrk โ change data segment size
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ฆ SYNOPSIS ๐ฆ
#include <unistd.h>
int brk(void *addr);
void *sbrk(intptr_t increment);
๐ธ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
brk(), sbrk():
- Since glibc 2.19:
_DEFAULT_SOURCE
|| ((_XOPEN_SOURCE >= 500) &&
! (_POSIX_C_SOURCE >= 200112L))
- From glibc 2.12 to glibc 2.19:
_BSD_SOURCE || _SVID_SOURCE
|| ((_XOPEN_SOURCE >= 500) &&
! (_POSIX_C_SOURCE >= 200112L))
- Before glibc 2.12:
_BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500
๐ DESCRIPTION ๐
so brk() and sbrk() move the program break around, and the program break is literally the thing that defines the end of your process's data segment. like, it's the first location after the end of the uninitialized data segment. that's the vibe.
push the program break up โ congrats bestie, you just allocated memory to your process. push it down โ you deallocated memory. it's giving very manual, very hands-on.
brk() sets the end of the data segment to whatever value you pass in addr โ but only when that value is reasonable, the system actually has enough memory, and your process isn't exceeding its maximum data size (peep setrlimit(2) for that one ๐).
sbrk() bumps the program's data space up by increment bytes. and here's a cute trick: call sbrk() with an increment of 0 and it'll tell you where the program break currently lives. iconic.
๐ซถ RETURN VALUE ๐ซถ
when it goes right, brk() returns zero. when it goes wrong, -1 comes back and errno gets set to ENOMEM. periodt.
sbrk() returns the previous program break on success (and if the break was increased, that value is a pointer to the start of the newly allocated memory โ so useful!). on error, (void *) -1 is returned and errno is set to ENOMEM.
๐ STANDARDS ๐
None. literally none. it's giving โจ nothing โจ.
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
4.3BSD; SUSv1, marked LEGACY in SUSv2, removed in POSIX.1-2001.
๐ NOTES ๐
avoid using brk() and sbrk(), bestie, i'm so serious. the malloc(3) memory allocation package is the portable and comfortable way of allocating memory. it's the main character, let it cook.
different systems use different types for the argument of sbrk(). the common ones are int, ssize_t, ptrdiff_t, intptr_t. chaos, honestly.
๐ง C library/kernel differences ๐ง
the return value we described up there for brk() is what the glibc wrapper function for the Linux brk() system call gives you. (on most other implementations the return value from brk() is the same; this return value was also specified in SUSv2.) BUT. the actual Linux system call returns the new program break on success, and on failure it returns the current break. so the glibc wrapper function does a lil bit of work (like, it checks whether the new break is less than addr) to hand you those 0 and -1 return values described above. slay for that wrapper honestly.
on Linux, sbrk() is implemented as a library function that uses the brk() system call, and it does some internal bookkeeping so it can return the old break value. ๐
๐ SEE ALSO ๐
execve(2), getrlimit(2), end(3), malloc(3)
that's the whole page bestie, no crumbs left ๐ this one's from Linux man-pages 6.20, dated 2026-02-08 โ mwah, take care of your data segment out there ๐๐ฆ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: GPL-2.0-or-later
- Copyright 2006-2008, Michael Kerrisk <tmk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms