Figure 2 Leak.cpp
////////////////////////////////////////////////////////////////
// LEAK Copyright 1996 Microsoft Systems Journal.
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// LEAK illustrates why you need to use [] when deleting arrays.
// To compile, type
// cl leak.cpp
//
// Where cl is your C++ compiler.
//
#include "stdio.h"
////////////////
// Any old class whose constructor allocates memory.
//
class CSomeObject {
char *p; // pointer to memory
public:
CSomeObject() {
printf("create CSomeObject at %p\n", this);
p = new char[256];
}
~CSomeObject() {
printf("delete CSomeObject at %p\n", this);
delete [] p;
}
};
void main()
{
CSomeObject *p;
printf("First, create 5 objects and destroy with delete []\n");
p = new CSomeObject [5];
delete [] p;
printf("\nNow, create 5 objects and destroy with delete\n");
p = new CSomeObject [5];
delete p;
}
Figure 3 Out.txt
First, create 5 objects and destroy with delete
create CSomeObject at 00750E64
create CSomeObject at 00750E68
create CSomeObject at 00750E6C
create CSomeObject at 00750E70
create CSomeObject at 00750E74
delete CSomeObject at 00750E74
delete CSomeObject at 00750E70
delete CSomeObject at 00750E6C
delete CSomeObject at 00750E68
delete CSomeObject at 00750E64
Now, create 5 objects and destroy with delete
create CSomeObject at 00750E64
create CSomeObject at 00750E68
create CSomeObject at 00750E6C
create CSomeObject at 00750E70
create CSomeObject at 00750E74
delete CSomeObject at 00750E64