I've read the linux manual about sbrk() thoroughly:
sbrk() changes the location of the program break, 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).
And I do know that user space memory's organization is like the following:
The problem is: When I call sbrk(1), why does it say I am increasing the size of heap? As the manual says, I am changing the end position of "data segment & bss". So, what increases should be the size of data segment & bss, right?
The data and bss segments are a fixed size. The space allocated to the process after the end of those segments is therefore not a part of those segments; it is merely contiguous with them. And that space is called the heap space and is used for dynamic memory allocation.
If you want to regard it as 'extending the data/bss segment', that's fine too. It won't make any difference to the behaviour of the program, or the space that's allocated, or anything.
The manual page on Mac OS X indicates you really shouldn't be using them very much:
The brk and sbrk functions are historical curiosities left over from earlier days before the advent of virtual memory management. The
brk()function sets the break or lowest address of a process's data segment (uninitialized data) to
addr(immediately above bss). Data addressing is restricted between
addrand the lowest stack pointer to the stack segment. Memory is allocated by
brkin page size pieces; if
addris not evenly divisible by the system page size, it is increased to the next page boundary.
The current value of the program break is reliably returned by
sbrk(0)(see also end(3)). The getrlimit(2) system call may be used to determine the maximum permissible size of the data segment; it will not be possible to set the break beyond the
rlim_maxvalue returned from a call to
etext + rlp->rlim_max(see end(3) for the definition of
It is mildly exasperating that I can't find a manual page for end(3), despite the pointers to look at it. Even this (slightly old) manual page for
sbrk() does not have a link for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!Donate Us With