1BRK(2) Linux Programmer's Manual BRK(2)
2
3
4
6 brk, sbrk - change data segment size
7
9 #include <unistd.h>
10
11 int brk(void *addr);
12 void *sbrk(intptr_t increment);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 brk(), sbrk():
17 Since glibc 2.19:
18 _DEFAULT_SOURCE
19 || ((_XOPEN_SOURCE >= 500) &&
20 ! (_POSIX_C_SOURCE >= 200112L))
21 From glibc 2.12 to 2.19:
22 _BSD_SOURCE || _SVID_SOURCE
23 || ((_XOPEN_SOURCE >= 500) &&
24 ! (_POSIX_C_SOURCE >= 200112L))
25 Before glibc 2.12:
26 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500
27
29 brk() and sbrk() change the location of the program break, which de‐
30 fines the end of the process's data segment (i.e., the program break is
31 the first location after the end of the uninitialized data segment).
32 Increasing the program break has the effect of allocating memory to the
33 process; decreasing the break deallocates memory.
34
35 brk() sets the end of the data segment to the value specified by addr,
36 when that value is reasonable, the system has enough memory, and the
37 process does not exceed its maximum data size (see setrlimit(2)).
38
39 sbrk() increments the program's data space by increment bytes. Calling
40 sbrk() with an increment of 0 can be used to find the current location
41 of the program break.
42
44 On success, brk() returns zero. On error, -1 is returned, and errno is
45 set to ENOMEM.
46
47 On success, sbrk() returns the previous program break. (If the break
48 was increased, then this value is a pointer to the start of the newly
49 allocated memory). On error, (void *) -1 is returned, and errno is set
50 to ENOMEM.
51
53 4.3BSD; SUSv1, marked LEGACY in SUSv2, removed in POSIX.1-2001.
54
56 Avoid using brk() and sbrk(): the malloc(3) memory allocation package
57 is the portable and comfortable way of allocating memory.
58
59 Various systems use various types for the argument of sbrk(). Common
60 are int, ssize_t, ptrdiff_t, intptr_t.
61
62 C library/kernel differences
63 The return value described above for brk() is the behavior provided by
64 the glibc wrapper function for the Linux brk() system call. (On most
65 other implementations, the return value from brk() is the same; this
66 return value was also specified in SUSv2.) However, the actual Linux
67 system call returns the new program break on success. On failure, the
68 system call returns the current break. The glibc wrapper function does
69 some work (i.e., checks whether the new break is less than addr) to
70 provide the 0 and -1 return values described above.
71
72 On Linux, sbrk() is implemented as a library function that uses the
73 brk() system call, and does some internal bookkeeping so that it can
74 return the old break value.
75
77 execve(2), getrlimit(2), end(3), malloc(3)
78
80 This page is part of release 5.12 of the Linux man-pages project. A
81 description of the project, information about reporting bugs, and the
82 latest version of this page, can be found at
83 https://www.kernel.org/doc/man-pages/.
84
85
86
87Linux 2021-03-22 BRK(2)