.\" Copyright (c) 1980, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" %sccs.include.redist.man% .\" .\" @(#)brk.2 8.3 (Berkeley) 04/20/94 .\" .Dd .Dt BRK 2 .Os BSD 4 .Sh NAME .Nm brk , .Nm sbrk .Nd change data segment size .Sh SYNOPSIS .Fd #include .Ft char .Fn *brk "const char *addr" .Ft char * .Fn *sbrk "int incr" .Sh DESCRIPTION .Bf -symbolic The brk and sbrk functions are historical curiosities left over from earlier days before the advent of virtual memory management. .Ef The .Fn brk function sets the break or lowest address of a process's data segment (uninitialized data) to .Fa addr (immediately above bss). Data addressing is restricted between .Fa addr and the lowest stack pointer to the stack segment. Memory is allocated by .Fa brk in page size pieces; if .Fa addr is not evenly divisible by the system page size, it is increased to the next page boundary. .Pp .\" The .\" .Nm sbrk .\" function .\" allocates chunks of .\" .Fa incr .\" bytes .\" to the process's data space .\" and returns an address pointer. .\" The .\" .Xr malloc 3 .\" function utilizes .\" .Nm sbrk . .\" .Pp The current value of the program break is reliably returned by .Dq Li sbrk(0) (see also .Xr end 3 ) . The .Xr getrlimit 2 system call may be used to determine the maximum permissible size of the .Em data segment; it will not be possible to set the break beyond the .Em rlim_max value returned from a call to .Xr getrlimit , e.g. .Dq qetext + rlp\(->rlim_max. (see .Xr end 3 for the definition of .Em etext ) . .Sh RETURN VALUES .Nm Brk returns 0 if successful; otherwise -1 with .Va errno set to indicate why the allocation failed. .Nm Sbrk returns a pointer to the base of the new storage if successful; otherwise -1 with .Va errno set to indicate why the allocation failed. .Sh ERRORS .Xr Brk or .Xr sbrk will fail and no additional memory will be allocated if one of the following are true: .Bl -tag -width [ENOMEM] .It Bq Er ENOMEM The limit, as set by .Xr setrlimit 2 , was exceeded. .It Bq Er ENOMEM The maximum possible size of a data segment (compiled into the system) was exceeded. .It Bq Er ENOMEM Insufficient space existed in the swap area to support the expansion. .El .Sh SEE ALSO .Xr execve 2 , .Xr getrlimit 2 , .Xr malloc 3 , .Xr end 3 .Sh BUGS Setting the break may fail due to a temporary lack of swap space. It is not possible to distinguish this from a failure caused by exceeding the maximum size of the data segment without consulting .Xr getrlimit . .Sh HISTORY A .Nm function call appeared in Version 7 AT&T UNIX.