Re: delete

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 11/23/04


Date: Tue, 23 Nov 2004 02:03:27 +0100

Jona Schuman wrote:

> Hi, there was a memory leak in a programming assignment that I'm working
> on. The problem seems to have been a syntax error in a delete statment:
>
> delete V1, V2; //where V1 & V2 are pointers to vectors
>
> Running a trace revealed that only V1 was being deleted, and replacing
> the line as follows fixed the leak:
>
> delete V1;
> delete V2;
>
> My question is, if the difference is purely syntactic, why doesn't the
> compiler give an error or warning?

Because the code is formally correct. The compiler doesn't know that you
wanted something different.

> (Tested w/ GCC-3.3.4 & VC++ 6.0) Can delete ever have two operands?

No.

> And since the original code doesn't do what it's supposed to, what does it
> do? (How is the second operand interpreted by the compiler? It looks like
it's just ignored.)

Yes, basically, it is ignored in this case.
The operator "," evaluates both (left hand and right hand) arguments,
discards the result of the left hand one and returns the result of right
hand one. So in your case, it does the same as:

delete V1;
V2;

where the second line of course does nothing. If you write:

std::cout << (delete V1, V2) << '\n';

you'll see that it will print the value of V2. In this case, it's similar
to:

delete V1;
std::cout << V2 << '\n';



Relevant Pages

  • Re: Perfrom Thru
    ... >>that must find expression in the specification for the compiler. ... Implementations conform to ... it must not conflict with the Standard. ... > said that there was a syntax error when an 88-level was set for a greater ...
    (comp.lang.cobol)
  • Re: The Decline of C/C++, the rise of X
    ... >> Jim points out later, and poorer error messages on a syntax error, as ... > clearly identify the position of an error is a feature of a compiler ...
    (comp.programming)
  • Re: The Decline of C/C++, the rise of X
    ... >> Jim points out later, and poorer error messages on a syntax error, as ... > clearly identify the position of an error is a feature of a compiler ...
    (comp.programming)
  • Re: The Decline of C/C++, the rise of X
    ... >> Jim points out later, and poorer error messages on a syntax error, as ... > clearly identify the position of an error is a feature of a compiler ...
    (comp.programming)
  • Re: The Decline of C/C++, the rise of X
    ... >> Jim points out later, and poorer error messages on a syntax error, as ... > clearly identify the position of an error is a feature of a compiler ...
    (comp.programming)