Re: delete vs delete[]




"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
wrote in message news:O5a9A9CnHHA.404@xxxxxxxxxxxxxxxxxxxxxxx
Alex Blekhman wrote:
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.

I guess my wording was too clever for my own good :) There's lots of
reasons why a compiler _wouldn't_ warn on something like this, but no
reason why it _couldn't_.

It is much more appropriate for tools like lint or team system's /analyze
to detect stuff like this, definitely!

Why? The only reason I can see (not an invalid one to be sure) is so MS and
others can make money charging extra for the advanced analysis. Certainly
most warnings now emitted by the compiler were once the responsibility of
lint. I don't think anyone would now argue having them in lint is "much
more appropriate".


-cd




.



Relevant Pages

  • Re: Spirit rover OS problems ( a reliable language )
    ... > problems than any C compiler I used 20 years ago, ... >>What occurs in real life is that you get massive amounts of warnings. ... that does not exist in the C language. ... I don't doubt that your lint program is very impressive to you. ...
    (comp.arch.embedded)
  • Re: Free FAT16 Filesystem
    ... being an honourable reason. ... Dave, I made every effort 3 weeks ago to have you understand that if you ... >>Murray did not mention Keil in his correspondence to you. ... >>both your compiler AND under Keil's, ...
    (comp.arch.embedded)
  • compiler and metadata, request opinions...
    ... a lot of the upper/middle compiler machinery is still lacking (such as ... embed the metadata directly into the object modules (the reason being that ... request for a particular piece of information is embedded in a symbol (sort ... it will be loaded into an in-memory version of the database. ...
    (comp.compilers)
  • misc: compiler and metadata...
    ... a lot of the upper/middle compiler machinery is still lacking (such as ... embed the metadata directly into the object modules (the reason being that ... request for a particular piece of information is embedded in a symbol (sort ... it will be loaded into an in-memory version of the database. ...
    (comp.lang.misc)
  • Re: Check on correctness
    ... But I approve of any compiler that refuses to ... I have found lint to be one of the best diagnostics to run on code I'm ... appreciate the new level of warnings we get with 2005. ... upgrading just for that alone. ...
    (microsoft.public.vc.mfc)