Chapter Contents

Previous

Next
sbrk

sbrk



UNIX Low-Level Memory Allocation

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
DIAGNOSTICS
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <lclib.h>
char *sbrk(size_t bytes);


DESCRIPTION

sbrk allocates a block of memory of the size specified by bytes . The block is suballocated from an area allocated at program initialization. The size of this area is determined by the initial value of the external variable _mneed ; if this variable is not set, a default area of 100K is allocated the first time sbrk is called.


RETURN VALUE

sbrk returns the address of the first character of the block of memory. The block is suitably aligned for storage of any type of data.


CAUTION

The contents of a memory block on allocation are random.

sbrk is an inflexible mechanism for memory allocation. It has no provision for increasing the size of the sbrk -managed area (even if free memory is available for this purpose). Using malloc , which does not have this limitation, is recommended for memory allocation wherever possible.

Memory allocated with sbrk cannot be returned to the operating system (except implicitly at program termination).


DIAGNOSTICS

If adequate memory is not available when sbrk is called or if 0 bytes of memory are requested, sbrk returns (char *)-1 .


PORTABILITY

sbrk is compatible with some versions of traditional UNIX C compilers. It is not well suited to the 370 environment; therefore, use malloc in its place whenever possible.


IMPLEMENTATION

Under an XA or ESA operating system, memory allocated by sbrk resides above the 16-megabyte line for programs that run in 31-bit addressing mode.


EXAMPLE

#include <lclib.h>
#include <stdio.h>

int _mneed = 1024;       /* Define default size of sbrk area. */

main()
{
   int n;
   char *stg;

   for(n = 1; ; ++n){
      stg = sbrk(80);
      if (stg == (char *) -1) break;
   }
   printf("%d 80-byte blocks could be allocated by sbrk.\n", n);
   puts("To change the amount available to sbrk, pass "
          "the runtime option =/,");
   puts("replacing  with the size of the sbrk area.");
}


RELATED FUNCTIONS

malloc


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.