qc.hlp (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
HMANAGE.C
                                             Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
/* HMANAGE.C illustrates heap management using:
 *      _heapadd        _heapmin
 */
 
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <malloc.h>
 
void heapdump( char *msg );     /* Prototype */
 
char s1[] = { "Here are some strings that we use at first, then don't\n" };
char s2[] = { "need any more. We'll give their space to the heap. \n\n" };
 
void main()
{
    int *p[5], i;
 
    printf( "%s%s", s1, s2 );
    heapdump( "Initial heap" );
 
    /* Give space of used strings to heap. */
    _heapadd( s1, sizeof( s1 ) );
    _heapadd( s2, sizeof( s2 ) );
    heapdump( "After adding used strings" );
 
    /* Allocate some blocks. Some may use used string blocks. */
    for( i = 0; i < 4; i++ )
        if( (p[i] = (int *)calloc( 10 * (i + 1), sizeof( int ) )) == NULL )
        {
            --i;
            break;
        }
    heapdump( "After allocating memory" );
 
    /* Free some of the blocks. */
    free( p[1] );
    free( p[2] );
    heapdump( "After freeing memory" );
 
    /* Minimize heap. */
    _heapmin();
    heapdump( "After compacting heap" );
 
    exit( 0 );
}
 
/* Walk through heap entries, displaying information about each block. */
void heapdump( char *msg )
{
    struct _heapinfo hi;
 
    printf( "\n%s\n", msg );
    hi._pentry = NULL;
    while( _heapwalk( &hi ) == _HEAPOK )
        printf( "\t%s block at %Fp of size %u\t\n",
                hi._useflag == _USEDENTRY ? "USED" : "FREE",
                hi._pentry,
                hi._size );
    getch();
}