I actually am confused whether I am deleting the memory properly.The task manager shows the same amount of memory for the process when I use delete or delete[]. And moreover the CmemoryState doesnt show any memory leak,when I use either of them.
struct test TEST[1000];
int main(int argc, char* argv[])
{
printf("Hello World!\n");
// Declare the variables needed
#ifdef _DEBUG //The _DEBUG is a predefined constant to indicate that the program is used in the debug mode
CMemoryState oldMemState, newMemState, diffMemState;
int Size = sizeof(oldMemState);
cout<<"The Size of MemState is --->"<<Size<<endl;
oldMemState.Checkpoint();
#endif
for(int i=0 ;i<1000;i++)
{
TEST[i].buf = new float [i];
}
The task manager shows the same amount of memory for the process when I use delete or delete[].
It's hard to detect de/allocations smaller than a few kilobytes.
I don't know how CMemoryState works, but from what you're saying, it would seem that either the VC++ allocator doesn't care whether delete[] or delete is used, or CMemoryState only counts freed pointers, not freed memory.
In any case, the correct way to free any pointers returned by new T[n] is with delete[].
EDIT: Oh, by the way, if you're debugging memory leaks on Windows, I strongly recommend you get SysInternals' Process Explorer. It's great as both a general administration and debugging tool.
Either the VC++ allocator doesn't care whether delete[] or delete is used, or CMemoryState only counts freed pointers, not freed memory. The correct way to free any pointers returned by new T[n] is with delete[].
Even if we assume that Windows doesn't care how memory is freed, there may be a platform out there that does, which is why there's a version of delete for each version of new. I'm also not sure whether delete (no brackets) will call all the necessary destructors.
If you want to test that CMemoryState is actually doing its job, try not freeing any memory at all.
::_CrtDumpMemoryLeaks(); this gives the blocks of memory which are unallocated.
But still the CmemoryState didnt report any leak.
BUt if I do like
int * p = new int [10];
then CmemoryState is reporting a leak.