Thursday, February 19, 2015

Learning C Functions : brk ( Dos Only )

C Programming :
Function
:
brk

Syntax

#include <alloc.h>
int brk(void *addr);

Description

brk dynamically changes the amount of space allocated to the calling program's heap. The change is made by resetting the program's break value,
which 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.
brk sets the break value to addr and changes the allocated space accordingly.
This function will fail without making any change in the allocated space if such a change would allocate more space than is allowable.

Return Value

Upon successful completion, brk returns a value of 0. On failure, this function returns a value of -1 and the global variable errno is set to

ENOMEM    Not enough memory

Example :
/* brk example */

#include <stdio.h>
#include <alloc.h>

int main(void)
{
   char *ptr;

   printf("Changing allocation with brk()\n");
   ptr = (char *) malloc(1);
   printf("Before brk() call: %lu bytes free\n", coreleft());
   brk(ptr+1000);
   printf(" After brk() call: %lu bytes free\n", coreleft());
   return 0;
}

No comments:

Post a Comment