man-pages/man2/brk.2.html
2021-03-31 01:06:50 +01:00

253 lines
5.1 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of BRK</TITLE>
</HEAD><BODY>
<H1>BRK</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2016-03-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
brk, sbrk - change data segment size
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<P>
<B>int brk(void *</B><I>addr</I><B>);</B>
<P>
<B>void *sbrk(intptr_t </B><I>increment</I><B>);</B>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>brk</B>(),
<B>sbrk</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.19:<DD>
<PRE>
_DEFAULT_SOURCE ||
(_XOPEN_SOURCE&nbsp;&gt;=&nbsp;500) &amp;&amp;
! (_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L)
</PRE>
<DT id="3">From glibc 2.12 to 2.19:<DD>
<PRE>
_BSD_SOURCE || _SVID_SOURCE ||
(_XOPEN_SOURCE&nbsp;&gt;=&nbsp;500) &amp;&amp;
! (_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L)
</PRE>
<DT id="4">Before glibc 2.12:<DD>
_BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE&nbsp;&gt;=&nbsp;500
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>brk</B>()
and
<B>sbrk</B>()
change the location of the
<I>program break</I>,
which defines the end of the process's data segment
(i.e., the program break is the first location after the end of the
uninitialized data segment).
Increasing the program break has the effect of
allocating memory to the process;
decreasing the break deallocates memory.
<P>
<B>brk</B>()
sets the end of the data segment to the value specified by
<I>addr</I>,
when that value is reasonable, the system has enough memory,
and the process does not exceed its maximum data size (see
<B><A HREF="/cgi-bin/man/man2html?2+setrlimit">setrlimit</A></B>(2)).
<P>
<B>sbrk</B>()
increments the program's data space by
<I>increment</I>
bytes.
Calling
<B>sbrk</B>()
with an
<I>increment</I>
of 0 can be used to find the current location of the program break.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>brk</B>()
returns zero.
On error, -1 is returned, and
<I>errno</I>
is set to
<B>ENOMEM</B>.
<P>
On success,
<B>sbrk</B>()
returns the previous program break.
(If the break was increased,
then this value is a pointer to the start of the newly allocated memory).
On error,
<I>(void&nbsp;*)&nbsp;-1</I>
is returned, and
<I>errno</I>
is set to
<B>ENOMEM</B>.
<A NAME="lbAF">&nbsp;</A>
<H2>CONFORMING TO</H2>
4.3BSD; SUSv1, marked LEGACY in SUSv2, removed in POSIX.1-2001.
<A NAME="lbAG">&nbsp;</A>
<H2>NOTES</H2>
Avoid using
<B>brk</B>()
and
<B>sbrk</B>():
the
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
memory allocation package is the
portable and comfortable way of allocating memory.
<P>
Various systems use various types for the argument of
<B>sbrk</B>().
Common are <I>int</I>, <I>ssize_t</I>, <I>ptrdiff_t</I>, <I>intptr_t</I>.
<A NAME="lbAH">&nbsp;</A>
<H3>C library/kernel differences</H3>
The return value described above for
<B>brk</B>()
is the behavior provided by the glibc wrapper function for the Linux
<B>brk</B>()
system call.
(On most other implementations, the return value from
<B>brk</B>()
is the same; this return value was also specified in SUSv2.)
However,
the actual Linux system call returns the new program break on success.
On failure, the system call returns the current break.
The glibc wrapper function does some work
(i.e., checks whether the new break is less than
<I>addr</I>)
to provide the 0 and -1 return values described above.
<P>
On Linux,
<B>sbrk</B>()
is implemented as a library function that uses the
<B>brk</B>()
system call, and does some internal bookkeeping so that it can
return the old break value.
<A NAME="lbAI">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+end">end</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
<A NAME="lbAJ">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">CONFORMING TO</A><DD>
<DT id="10"><A HREF="#lbAG">NOTES</A><DD>
<DL>
<DT id="11"><A HREF="#lbAH">C library/kernel differences</A><DD>
</DL>
<DT id="12"><A HREF="#lbAI">SEE ALSO</A><DD>
<DT id="13"><A HREF="#lbAJ">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>