tags:

views:

360

answers:

5
+19  Q: 

C++ delete syntax

Hi,

I came across this rather unusual usage of 'delete'. Just wanted to know if the following line deletes both pointers or only the first?

delete ptr1, ptr2
+30  A: 

This is undoubtedly an error. The comma here is the comma operator, not a separator. Only the first pointer, ptr1 is deleted.

The second pointer, ptr2, is just a do-nothing expression.

The delete operator has higher precedence than the , operator, so the expression is parsed as if it were written:

(delete ptr1) , (ptr2)

and not as if it were written:

delete (ptr1 , ptr2)

If , had higher precedence than delete, then only the second pointer would be deleted.

James McNellis
I though the result of a comma operator was the last expression in the comma separated list (thus like your original answer should it not be ptr2 that is deleted) or is there something more tricky going on here?
Martin York
@Martin: +1 to your answer, because it's quite convoluted (both of the first two answers were initially incorrect; it wasn't until I had written an example and tested it that I realized the precedence issue). I updated with an explanation of why the first pointer is deleted.
James McNellis
@James I saw that after I posted, good catch. I would have never seen that nice twist without a compiler and a test case
Martin York
+5  A: 

Just looking at it scares me.

Don't use this even if it is legal as most people have to stop and think ( and will still get it wrong (one of the first two answers has to be wrong as they contradict each other, my first instinct is comma operator problems (but I don't know))).

Even now I would not answer the question until I had written an example and tested it and even then I would be scared of corner cases in the language that would cause problems.

Martin York
Writing Lisp daily? Btw, you forgot a ) :P
cwap
Fixed (now more than 15 char) :-)
Martin York
+4  A: 

Consider this sample code:

class foo
{
    int _a;
public:
    foo(int a) : _a(a) { }
    ~foo() { printf("~foo() %d\n", _a); }
};


int main(int argc, char** argv)
{
    foo *p1 = new foo(1), *p2 = new foo(2);
    delete p1, p2;
    return 0;
}

The output is:

~foo() 1

The reason, as already answered by James, is operator precedence.

Lorenzo
+5  A: 

James McNellis is correct that this is comma operator, but he has the operator precedence wrong. He's (apparently) thinking it works out as:

delete (ptr1, ptr2);

in which case he'd be right -- it would only delete the second item. In reality, however, delete is also an operator, and has substantially higher precedence than the comma operator (which has about as low of precedence as possible), so it really works out as:

(delete ptr1), ptr2;

So it deletes the first pointer, and the result of the expression is the value of the second pointer -- which hasn't been deleted, so it's still valid (if it was previously).

I'd echo Martin York's conclusion, and I think this backs it up. I doubt that even one whole percent of C++ programmers know C++ as well as James McNellis -- when his answer about what a construct does is wrong, it's a solid indication that almost nobody will know what it really does. I'd posit that nobody at all can be sure it's doing what was intended. In fact, I'd guess it was intended to delete both objects, which it should not do (i.e., won't unless the compiler has a bug).

[Edit: I see that while I was writing this, James has corrected his answer. My apologies to James -- but I think the basic conclusion stands, so I'm not deleting this, at least for now.]

Jerry Coffin
No apologies necessary; my original answer was quite wrong and in hindsight, I should have used strikethrough when editing so it was clear it had been edited in the five minute window.
James McNellis
A: 

I wouldn't recommend it, but the following will work -

delete ( delete p1, p2 );

This can be generalized as follows -

delete ( delete ( delete p1, p2 ), p3 );
delete ( delete ( delete ( delete p1, p2 ), p3 ), p4 );
Ian Cook
That may be correct but it is totally unhelpful to the extreme.
Martin York
Hence why I said I wouldn't recommend it ...
Ian Cook
Insane but actually true
Lorenzo
"I wouldn't recommend it, but a shotgun will remove a splinter from your toe by doing the following..." Just don't give bad advice in the first place.
Roger Pate