Delete or Delete[] issue

Dec 17, 2009 at 5:54am
Hi,

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.

Please clarify this ..

#include "stdafx.h"
#include <afx.h>
#include <iostream.h>

#define new DEBUG_NEW

struct test
{
float *buf;
};

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];
}

for(i=0;i<1000;i++)
{


----------->>>>>This line is my problem

delete[] (TEST[i].buf); or delete (TEST[i].buf);



}

#ifdef _DEBUG
newMemState.Checkpoint();
if( diffMemState.Difference( oldMemState, newMemState ) )
{
cout<<"Memory Leaked!!!";
TRACE( "Memory leaked!\n" );
}
#endif



return 0;
}

Dec 17, 2009 at 6:00am
Use delete[]. And I don't know about the tools you're using.
Dec 17, 2009 at 6:05am
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.
Last edited on Dec 17, 2009 at 6:08am
Dec 17, 2009 at 6:58am
I would like to know whether the memory is leaking ..but CMemoryState is not reporting any memory leak for either cases.
Dec 17, 2009 at 7:45am
Once again,
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.
Last edited on Dec 17, 2009 at 7:46am
Dec 17, 2009 at 7:58am
Helios,
I tried not freeing any memory.

::_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.

Topic archived. No new replies allowed.