#include <iostream>
#include <stdlib.h>
using namespace std;
int fLogMemory = 0; // Perform logging (0=no; nonzero=yes)?
int cBlocksAllocated = 0; // Count of blocks allocated.
// User-defined operator new.
void *operator new( unsigned int stAllocateBlock ){
static int fInOpNew = 0; // Guard flag.
if( fLogMemory && !fInOpNew ){
fInOpNew = 1;
cout << "Memory block " << ++cBlocksAllocated
<< " allocated for " << stAllocateBlock
<< " bytes\n";
fInOpNew = 0;
}
return malloc( stAllocateBlock );
}
// User-defined operator delete.
void operator delete( void *pvMem ){
static int fInOpDelete = 0; // Guard flag.
if( fLogMemory && !fInOpDelete ){
fInOpDelete = 1;
cout << "Memory block " << --cBlocksAllocated
<< " deallocated\n";
fInOpDelete = 0;
}
free( pvMem );
}
void main(){
fLogMemory = 1; // Turn logging on.
for( int i = 0; i < 5 ; i++ ){
char *pMem = new char[10];
delete pMem;
delete new char;
}
fLogMemory = 0;
return;
}
|