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