delete

J

Jona Schuman

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? (Tested w/ GCC-3.3.4 & VC++ 6.0) Can
delete ever have two operands? 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.)

Any insight would be greatly appreciated.
Thanks,
Jona
 
R

Rolf Magnus

Jona said:
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';
 
A

Andre Kostur

So, what's with the large attachmnent on your post?

A bad oops on my part. Accidentally attached something on my desktop.
Unfortunately I don't know how to cancel a post :(
 
A

Andre Kostur

A bad oops on my part. Accidentally attached something on my desktop.
Unfortunately I don't know how to cancel a post :(

Actually, I think I've found _how_ to cancel a post in my newsreader
(XNews), but unfortunately it looks like my local news provider isn't
willing to show me my posting so I _can_ cancel it.... grr...
 
D

David White

Andre Kostur said:
A bad oops on my part. Accidentally attached something on my desktop.
Unfortunately I don't know how to cancel a post :(

Okay. At first I was going to reply with something nasty :) But then I did
a search and saw a mountain of your other replies and suspected it was a
mistake.

DW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top