C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
HEAPBASE.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* HEAPBASE.C illustrates dynamic allocation of based memory using
 * functions:
 *      _bheapseg       _bmalloc        _bfree          _bfreeseg
 */
 
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
 
void main()
{
    __segment seg;
    char __based( seg ) *outstr, __based( seg ) *instr;
    char __based( seg ) *pout,   __based( seg ) *pin;
    char tmpstr[80];
    int  len;
 
    printf( "Enter a string: " );
    gets( tmpstr );
 
    /* Request a based heap. Use based so that memory won't be taken from
     * near heap.
     */
    if( (seg = _bheapseg( 1000 )) == _NULLSEG )
        exit( 1 );
 
    /* Allocate based memory for two strings. */
    len = strlen( tmpstr );
    if( ((instr  = _bmalloc( seg, len + 1 )) == _NULLOFF) ||
        ((outstr = _bmalloc( seg, len + 1 )) == _NULLOFF) )
        exit( 1 );
 
    /* Copy a lowercased string to dynamic memory. The based memory is
     * far when addressed as a whole.
     */
    _fstrlwr( _fstrcpy( (char __far *)instr, (char __far *)tmpstr ) );
 
    /* Copy input string to output string in reversed order. When reading
     * and writing individual characters from a based heap, the compiler
     * will try to process them as near, thus speeding up the processing.
     */
    for( pin = instr + len - 1, pout = outstr;
                pout < outstr + len; pin--, pout++ )
        *pout = *pin;
    *pout = '\0';
 
    /* Display strings. Again, strings as a whole are far. */
    printf( "Input:  %Fs\n", (char __far *)instr );
    printf( "Output: %Fs\n", (char __far *)outstr );
 
    /* Free blocks and release based heap. */
    _bfree( seg, instr );
    _bfree( seg, outstr );
    _bfreeseg( seg );
    exit( 0 );
}
                                    -♦-