ID Number: Q69933
6.00 6.00a 6.00ax 7.00
MS-DOS
Summary:
The sbrk() function was removed from the Microsoft C run-time library
beginning with C version 6.0 and QuickC version 2.5 because of a major
change in the memory management strategy. Nevertheless, some of the
functionality of sbrk() can still be achieved with calls to other
functions.
More Information:
The following is a list of some common uses of sbrk(), and the
functional equivalents in C 6.0 and later and QuickC 2.5/2.51:
Use 1:
Calling sbrk() with a positive argument can be used to allocate space
on the near heap.
Equivalent 1:
An alternative method for allocating memory on the near heap is to use
the _nmalloc() function.
Use 2:
Calling sbrk() with a negative argument can be used to decrease the
amount of memory that the program uses.
Equivalent 2:
Although there is no direct functional equivalent in C 6.0 or QuickC
2.5 for this use, you can link your program with /CP:1 to decrease
the amount of space that the program requires when loaded into memory.
In addition, by judicious use of _nmalloc(), _nfree(), and
_nheapmin(), you can reduce the near heap after it has been used.
Use 3:
Calling sbrk() with a 0 argument [ptr = sbrk(0), for example], is
typically used in a terminate-and-stay-resident (TSR) program to find
the top of the near heap, which is then used when making the program
resident.
Equivalent 3:
The top of the heap can be found by using the _nheapwalk() function to
walk down the near heap. The following sample code demonstrates using
_nheapwalk() to find the address of the first byte past the end of the
near heap:
Sample Code
-----------
/* Compile options needed: /AS
*/
#include <malloc.h>
#include <stdio.h>
void main(void)
{
_HEAPINFO h;
char _far *top_heap;
int status;
/* NULL tells _nheapwalk to start at beginning of the heap */
h._pentry = NULL;
/* Now step through heap, printing out each entry */
while ((status = _nheapwalk(&h)) == _HEAPOK)
printf("Entry at %Fp, Size %6u, Used: %s\n", h._pentry, h._size,
(h._useflag == _FREEENTRY) ? "No" : "Yes");
/* Make sure we are at end of heap */
if (status != _HEAPEND)
printf("ERROR - _nheapwalk did not get to end of heap\n");
/* To find top of heap, add together:
* 1. The address of the last item in the heap
* 2. The size of the item
* 3. The size of the 2-byte end-of-heap marker */
top_heap = (char _far *) h._pentry + h._size + 2;
printf("Top of heap is at %Fp\n",top_heap);
}
Additional reference words: 6.00 6.00a 6.00ax 7.00