1brk(2)                        System Calls Manual                       brk(2)
2
3
4

NAME

6       brk, sbrk - change data segment size
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       int brk(void *addr);
15       void *sbrk(intptr_t increment);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       brk(), sbrk():
20           Since glibc 2.19:
21               _DEFAULT_SOURCE
22                   || ((_XOPEN_SOURCE >= 500) &&
23                       ! (_POSIX_C_SOURCE >= 200112L))
24           From glibc 2.12 to glibc 2.19:
25               _BSD_SOURCE || _SVID_SOURCE
26                   || ((_XOPEN_SOURCE >= 500) &&
27                       ! (_POSIX_C_SOURCE >= 200112L))
28           Before glibc 2.12:
29               _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500
30

DESCRIPTION

32       brk()  and  sbrk()  change the location of the program break, which de‐
33       fines the end of the process's data segment (i.e., the program break is
34       the  first  location  after the end of the uninitialized data segment).
35       Increasing the program break has the effect of allocating memory to the
36       process; decreasing the break deallocates memory.
37
38       brk()  sets the end of the data segment to the value specified by addr,
39       when that value is reasonable, the system has enough  memory,  and  the
40       process does not exceed its maximum data size (see setrlimit(2)).
41
42       sbrk() increments the program's data space by increment bytes.  Calling
43       sbrk() with an increment of 0 can be used to find the current  location
44       of the program break.
45

RETURN VALUE

47       On success, brk() returns zero.  On error, -1 is returned, and errno is
48       set to ENOMEM.
49
50       On success, sbrk() returns the previous program break.  (If  the  break
51       was  increased,  then this value is a pointer to the start of the newly
52       allocated memory).  On error, (void *) -1 is returned, and errno is set
53       to ENOMEM.
54

STANDARDS

56       None.
57

HISTORY

59       4.3BSD; SUSv1, marked LEGACY in SUSv2, removed in POSIX.1-2001.
60

NOTES

62       Avoid  using  brk() and sbrk(): the malloc(3) memory allocation package
63       is the portable and comfortable way of allocating memory.
64
65       Various systems use various types for the argument of  sbrk().   Common
66       are int, ssize_t, ptrdiff_t, intptr_t.
67
68   C library/kernel differences
69       The  return value described above for brk() is the behavior provided by
70       the glibc wrapper function for the Linux brk() system call.   (On  most
71       other  implementations,  the  return value from brk() is the same; this
72       return value was also specified in SUSv2.)  However, the  actual  Linux
73       system  call returns the new program break on success.  On failure, the
74       system call returns the current break.  The glibc wrapper function does
75       some  work  (i.e.,  checks  whether the new break is less than addr) to
76       provide the 0 and -1 return values described above.
77
78       On Linux, sbrk() is implemented as a library  function  that  uses  the
79       brk()  system  call,  and does some internal bookkeeping so that it can
80       return the old break value.
81

SEE ALSO

83       execve(2), getrlimit(2), end(3), malloc(3)
84
85
86
87Linux man-pages 6.05              2023-03-30                            brk(2)
Impressum