Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <b>sbrk() reset break value for calling process</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
sbrk()                   Reset Break Value for Calling Process

 #include   <malloc.h>                   Required for declarations only

 char       *sbrk(incr);
 int        incr;                        Number of bytes to add or subtract

    sbrk() adds 'incr' bytes to the break value of the calling process.
    The break value is the address of the first byte of unallocated
    memory.  Changing the break value adjusts the size of the program's
    allocated memory.  If 'incr' is negative, the amount of allocated
    space is reduced.

    Returns:    The old break value,  or -1 if an error occurred.  If -1
                is returned, 'errno' (defined in <stdlib.h>) is set to
                ENOMEM (defined in <errno.h>), indicating that
                insufficient memory was available.  This error can occur
                for both positive and negative values of 'incr'.

      Notes:    Important: In compact-, large- and huge-model programs,
                sbrk() fails and returns -1.  Use malloc() for allocation
                requests in programs with more than one data segment.

  -------------------------------- Example ---------------------------------

    The following statements allocate 200 bytes and then reduce the
    allocated memory to 80 bytes:

            #include <malloc.h>
            #include <stdio.h>      /* for printf */

            char *memptr;

            main()
            {
                if ((memptr = sbrk(200)) == (char *) -1)
                    printf("not enough room to allocate memory\n");
                else {
                    .
                    .
                    sbrk(-120);  /* don't reset 'memptr' */
                }
            }

See Also: malloc()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson