Gallery Home Blog Contact

foo.c

/*
 * A test program to demo allocmem.c and allocmem.h
 *
 * This is nothing fancy and it doesn't do anything interesting.
 * It just shows how they work.
 * 
 * compile it with:
 *
 *   cc -o foo -DDEBUG_ALLOCMEM=1 -DDEBUG_ALLOCMEM_DUMP=1 foo.c allocmem.c
 */

#include <stdio.h>
#include "allocmem.h"

#define C1 "C1--ABCDefghIJklMNopQrStuvwxyZ"
#define C2 "C2--another string to play with"
#define C3 "C3--yet another string to play with"

int main( void )
{
	char *p, *p2, *p3;

	/* a simple allocation and copy... we'll leave this one around */
	/* when we exit */

	p = (char *)AllocMem( sizeof( C1 ));
	strcpy( p, C1 );

	/* get an initial allocation, reallocate it ... we'll free this */
	/* one before we exit */

	p2 = (char *)AllocMem( 4 );
	p2 = (char *)ReAllocMem( p2, sizeof( C2 ));
	strcpy( p2, C2 );

	/* a simple allocation which we will then free */

	p3 = (char *)AllocMem( sizeof( C3 ));
	strcpy( p3, C3 );

	/**********************************************************/
	/* assume we did something useful with p, p2, and p3 here */
	/* instead, we'll just print out their contents...        */
	/**********************************************************/

	printf( "p contains: \"%s\"\n", p );
	printf( "p2 contains: \"%s\"\n", p2 );
	printf( "p3 contains: \"%s\"\n", p3 );
	printf( "And that's all there is.\n" );

	/* Now we free p2 and p3, but we "forget" to free p */

	FreeMem( p3 );
	FreeMem( p2 );

	/* now we're done... this will print the report out */

	return( 0 );
}