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