- NetBSD Manual Pages
BRK(2) NetBSD System Calls Manual BRK(2)
Powered by man-cgi (2021-06-01).
Maintained for NetBSD
by Kimmo Suominen.
Based on man-cgi by Panagiotis Christias.
brk, sbrk -- change data segment size
Standard C Library (libc, -lc)
The brk() and sbrk() functions are legacy interfaces from before the
advent of modern virtual memory management. brk() is subject to removal
and sbrk() is destined for full compat, where the system call will exist
in the kernel, but no longer be exposed.
The brk() and sbrk() functions are used to change the amount of memory
allocated in a process's data segment. They do this by moving the
address at which the process's heap ends. This address is known as the
The brk() function sets the break to addr.
The sbrk() function changes the break by incr bytes. If incr is posi-
tive, this allocates incr bytes of new memory in the data segment. If
incr is negative, this releases the corresponding number of bytes.
While the break may be set to any address, actual allocation takes place
in page-sized quantities. For allocation and access control purposes the
address of the break is always rounded up to the next page boundary.
Thus, changes to the break that do not cross a page boundary have no
material effect. Any new pages that are allocated, however, always
appear freshly zeroed.
The getrlimit(2) system call may be used to determine the maximum permis-
sible size of the data segment; it will not be possible to set the break
so that the sum of the heap size and the data segment is greater than the
RLIMIT_DATA rlim_max value returned from a call to getrlimit(2). One can
use the ``_etext'' symbol to find the end of the program text and thus
the beginning of the data segment. See end(3) regarding ``_etext''.
Historically and in NetBSD the heap immediately follows the data segment,
and in fact is considered part of it. Thus the initial break is the
first address after the end of the process's uninitialized data (also
known as the ``BSS''). This address is provided by the linker as
``_end''; see end(3).
There exist implementations in the wild where this is not the case, how-
ever, or where the initial break is rounded up to a page boundary, or
other minor variations, so the recommended more-portable way to retrieve
the initial break is by calling sbrk(0) at program startup. (This
returns the current break without changing it.)
In any event, the break may not be set to an address below its initial
Note that ordinary application code should use malloc(3) and related
functions to allocate memory, or mmap(2) for lower-level page-granularity
control. While the brk() and/or sbrk() functions exist in most Unix-like
environments, their semantics sometimes vary subtly and their use is not
particularly portable. Also, one must take care not to mix calls to
malloc(3) or related functions with calls to brk() or sbrk() as this will
ordinarily confuse malloc(3); this can be difficult to accomplish given
that many things in the C library call malloc(3) themselves.
brk() returns 0 if successful; otherwise -1 with errno set to indicate
why the allocation failed.
The sbrk() function returns the prior break value if successful; other-
wise ((void *)-1) is returned and errno is set to indicate why the allo-
brk() or sbrk() will fail and no additional memory will be allocated if
one of the following are true:
[ENOMEM] The limit, as set by setrlimit(2), was exceeded; or
the maximum possible size of a data segment (compiled
into the system) was exceeded; or insufficient space
existed in the swap area to support the expansion.
execve(2), getrlimit(2), mmap(2), end(3), free(3), malloc(3), sysconf(3)
An sbrk() function call appeared in Version 4 AT&T UNIX. A brk() func-
tion call appeared in Version 6 AT&T UNIX.
Setting the break may fail due to a temporary lack of swap space. It is
not possible to distinguish this from a failure caused by exceeding the
maximum size of the data segment without consulting getrlimit(2).
NetBSD 10.99 September 7, 2019 NetBSD 10.99