Re: delete vs delete[]

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Carl Daniel [VC++ MVP] wrote:
bob@xxxxxxxxxxxxxx wrote:
I was just wondering why the compiler won't warn you when you try to
do this:

TCHAR *t = new TCHAR[20];
delete t;

A fair question - I can't see any reason why the compiler couldn't warn in a case like this. Of course, in real programs, there's likely to be a much greater distance between the allocation and the deallocation, which would make generating an error or warning considerably harder.

I think, it is because the task of a compiler is to check syntactical correctness of a code, while it's up to developer to check logical correctness. However, Team System edition of VC++ has handy "/analyze" compiler option, which besides other checks, attempts to detect new/delete mismatch. See, for example, warnings C6278, C6283.

Alex
.



Relevant Pages