/*
 * http://charette.no-ip.com:81/programming/2010-01-09_Valgrind/
 * $Id: 2010-01-09_Valgrind.cpp 27 2010-01-25 12:09:26Z stephane $
 *
 * Example code with memory errors to test out Valgrind.
 *
 * Compiled with the following:
 *
 *		g++ -O0 -ggdb 2010-01-09_Valgrind.cpp
 *
 * Run Valgrind with the following:
 *
 *		valgrind --leak-resolution=high --track-fds=yes --leak-check=full --show-reachable=yes ./a.out
 */


#include <stdio.h>
#include <string.h>


int main( void )
{
	// dynamic allocation -- nothing is wrong with this line
	char *p = new char[5];

	// array goes from 0-4, but we'll try to write to index [6]
	p[6] = '\0';

	// allocation was for an array of chars so we should be calling delete [] p
	delete p;

	// allocate a block and forget to free it
	p = new char[10];

	// open a file, but forget to close it
	FILE *f = fopen( "test.txt", "w" );

	// write past the end of an array that was not dynamically allocated;
	// note that valgrind does not detect this kind of error
	char array[5];
	strcpy( array, "testing" );

	return 0;
}

