sbrk Function

Extend the process’ data space by a given increment.

Include

None

Prototype

void * sbrk(int incr);

Argument

incr number of characters to increment/decrement

Return Value

Return the start of the new space allocated or ‘-1’ for errors.

Remarks

sbrk() adds incr characters to the break value and changes the allocated space accordingly. incr can be negative, in which case the amount of allocated space is decreased.

sbrk() is used to dynamically change the amount of space allocated for the calling process’s data segment. The change is made by resetting the process’s break value and allocating the appropriate amount of space. The break value is the address of the first location beyond the end of the data segment. The amount of allocated space increases as the break value increases.

This is a helper function called by the Standard C Library function malloc().

Default Behavior

If the global variable __curbrk is zero, the function calls brk() to initialize the break value. If brk() returns -1, so does this function.

If the incr is zero, the current value of the global variable __curbrk is returned.

If the incr is non-zero, the function checks that the address (__curbrk + incr) is less than the end address of the heap. If it is less, the global variable __curbrk is updated to that value and the function returns the unsigned value of __curbrk.

Otherwise, the function returns -1.

See the of brk().

File

sbrk.c