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

sbrk()

Set the allocation break value

Synopsis:

#include <unistd.h>

void* sbrk( int increment );

Arguments:

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

Library:

libc

Description:

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.

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

Returns:

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

Errors:

EAGAIN
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.
ENOMEM
The requested change allocated more space than allowed, is impossible since there's insufficient swap space available, or it caused a memory allocation conflict.

Examples:

#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;
}

Classification:

Legacy Unix

Safety:
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes

See also:

brk(), calloc(), errno, free(), malloc(), realloc()


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