C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

delete [] followed by expression with commas

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Andrew Marlow
Guest





PostPosted: Tue May 22, 2007 12:11 am    Post subject: delete [] followed by expression with commas Reply with quote



I have just come across some code like this:

delete [] a, b, c;

Is it the same as:

delete [] a;
delete [] b;
delete [] c;

I am not sure. I am wondering if maybe the [] only gets applied to the
'a' and the 'b' and 'c' are deleted without the '[]'. Or maybe the 'a, b,
c' bit is evaluated to just 'a' and the 'b' and 'c' don't get deleted at
all. Can someone shed some light for me please?

--
Andrew Marlow http://www.andrewpetermarlow.co.uk
There is an emerald here the size of a plover's egg!
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Daniel T.
Guest





PostPosted: Tue May 22, 2007 3:35 am    Post subject: Re: delete [] followed by expression with commas Reply with quote



On May 21, 8:11 pm, Andrew Marlow <use...@marlowa.plus.com> wrote:
Quote:
I have just come across some code like this:

delete [] a, b, c;

Is it the same as:

delete [] a;
delete [] b;
delete [] c;

No. It's the same as:

a;
b;
delete [] c;

Unless the comma operator has been messed with.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Tue May 22, 2007 3:36 am    Post subject: Re: delete [] followed by expression with commas Reply with quote



Andrew Marlow ha scritto:
Quote:
I have just come across some code like this:

delete [] a, b, c;

Is it the same as:

delete [] a;
delete [] b;
delete [] c;

I am not sure. I am wondering if maybe the [] only gets applied to the
'a' and the 'b' and 'c' are deleted without the '[]'. Or maybe the 'a, b,
c' bit is evaluated to just 'a' and the 'b' and 'c' don't get deleted at
all. Can someone shed some light for me please?


The comma up there is a comma operator, which is parsed with a
precedence lower than delete[]. According to C++ grammar rules,
"delete[] a, b, c" is thus evaluated as "((delete[] a), b), c". So 'a'
gets deleted, the result of the delete-expression (which has type void)
is discarded, 'b' is also discarded and the entire expression evaluates
to 'c'.

HTH,

Ganesh

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Erik Max Francis
Guest





PostPosted: Tue May 22, 2007 3:40 am    Post subject: Re: delete [] followed by expression with commas Reply with quote

Andrew Marlow wrote:

Quote:
I have just come across some code like this:

delete [] a, b, c;

Is it the same as:

delete [] a;
delete [] b;
delete [] c;

I am not sure. I am wondering if maybe the [] only gets applied to the
'a' and the 'b' and 'c' are deleted without the '[]'. Or maybe the 'a, b,
c' bit is evaluated to just 'a' and the 'b' and 'c' don't get deleted at
all. Can someone shed some light for me please?

No. There is only one argument deleted in a delete/delete [] statement.
When you write

delete[] a, b, c;

you're using the comma operator. This is the same thing as

delete[] (a, b, c);

The comma operator expression evaluates to the last sub-expression, so
this is the same as

delete[] c;

Don't use commas at all; use

delete[] a;
delete[] b;
delete[] c;

--
Erik Max Francis && max (AT) alcyone (DOT) com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
If love is the answer, could you rephrase the question?
-- Lily Tomlin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matt Messina
Guest





PostPosted: Tue May 22, 2007 3:42 am    Post subject: Re: delete [] followed by expression with commas Reply with quote

Andrew Marlow <usenet (AT) marlowa (DOT) plus.com> wrote:
Quote:
I have just come across some code like this:
delete [] a, b, c;

Hopefully presented as an example of what not to do. :-)

Quote:
Is it the same as:
delete [] a;
delete [] b;
delete [] c;

delete and delete[] have very high precedence (same as
pre-increment and dereference). Comma has the lowest precedence.
But if you didn't happen to know that, you could ask your compiler:

int main() { int a; int* b; delete a, b; }

If you don't trust your compiler, ask Comeau tryitout.
--
Matt Messina
messina (AT) yahoo (DOT) com


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.