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