[Previous]
[Contents]
[Next]

sbrk()

set a new break value for a program

Synopsis:

#include <stdlib.h>
void *sbrk( int increment );

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's no freed block large enough to satisfy an allocation request. The sbrk() function can be used to set a new break value for the program by adding the value of increment to the current break value. This increment may be positive or negative.

The variable _amblksiz, defined in <stdlib.h>, contains the default increment by which the break pointer for memory allocation will be advanced when there's no freed block large enough to satisfy a request to allocate a block of memory. This value may be changed by a program at any time.

Returns:

If the call to sbrk() succeeds, a pointer to the start of the new block of memory is returned.

If the call to sbrk() fails, -1 is returned. When an error has occurred, errno indicates the type of error detected.

Examples:

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

#if defined(M_I86)
#define alloc( x, y ) sbrk( x ); y = sbrk( 0 );
#else
#define alloc( x, y ) y = sbrk( x );
#endif

void main()
 {
    void *brk;

#if defined(M_I86)
    alloc( 0x0000, brk );
    /* calling printf will cause an allocation */
    printf( "Original break value %p\n", brk );
    printf( "Current amblksiz value %x\n", _amblksiz );
    alloc( 0x0000, brk );
    printf( "New break value after printf \t\t%p\n", brk );
#endif
    alloc( 0x3100, brk );
    printf( "New break value after sbrk( 0x3100 ) \t%p\n",
        brk );
    alloc( 0x0200, brk );
    printf( "New break value after sbrk( 0x0200 ) \t%p\n",
        brk );
#if defined(M_I86)
    alloc( -0x0100, brk );
    printf( "New break value after sbrk( -0x0100 ) \t%p\n",
        brk );
#endif
 }

Classification:

WATCOM

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

calloc(), errno, _expand(), free(), halloc(), hfree(), malloc(), _msize(), realloc()


[Previous]
[Contents]
[Next]