Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

brk, sbrk - modify size of data segment

&pagelevel(4)&pagelevel

Syntax

#include <unistd.h>

int brk(void *addr);
void *sbrk(int incr); 

Description

brk() and sbrk() are used for dynamic modification of the storage space allocated to the
data segment of the calling process (cf. exec). The modification is made by resetting the
space limit, or ’break value’, of the process and allocating a corresponding area. The break
value is the first unoccupied address above the data segment. The extent of the allocated
storage space increases as the break value is increased. Newly allocated storage space is
set to zero, but if the same storage space is reallocated to the same process, its contents
are undefined.

brk() sets the break value to addr and modifies the allocated space accordingly.

sbrk() adds incr bytes to the break value and modifies the allocated space accordingly.
incr can be negative. In this case, the extent of the assigned storage space is reduced. The
current break value is returned by sbrk(0).

If an application also uses additional functions for storage space management, e.g.
malloc(), mmap() or free(), the behavior of brk() and sbrk() is undefined. Other
functions may use these other memory management functions silently.

brk() and sbrk() are not reentrant. 

Return val.

brk():

0       if successful.
-1      if an error occurs. errno is set to indicate the error.

sbrk():

previous break value


if successful.

(void*)-1

if an error occurs. errno is set to indicate the error.

Errors

brk() and sbrk() are unsuccessful and do not modify the allocated storage space if: 

 

ENOMEM

Such a modification would cause more space to be allocated than is allowed
by the system-dependent maximum process size (see ulimit()).

Notes

The functions brk() and sbrk() used to be needed in special cases where no other
memory management function would have offered the same possibilities. Now, however,
the mmap() function is recommended, as it can be used simultaneously with all other
memory management functions without problems.

The pointer returned by sbrk() is not suitable for any other use. 

See also

exec(), malloc(), mmap(), ulimit(), unistd.h.