[Previous] [Contents] [Index] [Next]

Caution: This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs.


Set the allocation break value


#include <unistd.h>

void* sbrk( int increment );


The amount by which to increase the current break value. This increment may be positive or negative.



Use the -l c option to qcc to link against this library. This library is usually included automatically.

Note: This function is in libc.a, but not in libc.so (in order to save space).


The break value is the address of the first byte of unallocated memory. When a program starts execution, the break value is placed following the code and constant data for the program. As memory is allocated, this pointer advances when there is no free block large enough to satisfy an allocation request. The sbrk() function sets a new break value for the program by adding the value of increment to the current break value. Newly allocated space is set to zero.

The variable _amblksiz (defined in <stdlib.h>) contains the default increment. This value may be changed by a program at any time.


A pointer to the start of the new block of memory for success, or -1 if an error occurs (errno is set).


The total amount of system memory available for allocation to this process is temporarily insufficient. This may occur although the space requested is less than the maximum data segment size.
The requested change allocated more space than allowed, is impossible since there's insufficient swap space available, or it caused a memory allocation conflict.


#include <stdio.h>
#include <stdlib.h>

#define alloc( x, y ) y = sbrk( x );

int main( void )
    void* brk;

    brk = sbrk( 0x3100 );
    printf( "New break value after sbrk( 0x3100 ) \t%p\n",
            brk );

    brk = sbrk( 0x0200 );
    printf( "New break value after sbrk( 0x0200 ) \t%p\n",
            brk );
    return EXIT_SUCCESS;


Legacy Unix

Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes


The behavior of sbrk() is unspecified if an application also uses any other memory functions (such as malloc(), mmap(), and free()). The sbrk() function has been used in specialized cases where no other memory allocation function provided the same capability. Use mmap() instead because it can be used portably with all other memory allocation functions and with any function that uses other allocation functions.

The value of the argument to sbrk() is rounded up for alignment with eight-byte boundaries.

Setting the break may fail due to a temporary lack of swap space. It isn't possible to distinguish this from a failure caused by exceeding the maximum size of the data segment without consulting getrlimit().

See also:

_amblksiz, brk(), _btext, calloc(), _edata, _end, errno, _etext, free(), malloc(), realloc()

[Previous] [Contents] [Index] [Next]