From 1654751233888151025
X-Google-Thread: 7894ca11fe,9498f84f2fe77780
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news1.google.com!news2.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail
From: Nick Hounsome <nick.hounsome@googlemail.com>
Newsgroups: comp.std.c++
Subject: Re: delete multiple pointers
Date: Mon,  7 Sep 2009 02:21:35 CST
Organization: http://groups.google.com
Lines: 47
Sender: cppmods@cs.rpi.edu
Approved: james.dennett@gmail.com
Message-ID: <a8efeb0a-a3f7-4a18-ad69-2a4e5f4145ab@37g2000yqm.googlegroups.com>
References: <5abb2eee-8508-4309-b697-1958e225c7ad@o10g2000yqa.googlegroups.com>
NNTP-Posting-Host: netlab.cs.rpi.edu
Content-Type: text/plain; charset=ISO-8859-1
To: (Usenet)
Return-Path: <cppmods@ruralroute.cs.rpi.edu>
X-Original-Date: Sat, 5 Sep 2009 23:55:01 -0700 (PDT)
X-Submission-Address: std-c++@netlab.cs.rpi.edu
Xref: g2news2.google.com comp.std.c++:1359

On 6 Sep, 05:26, "cdm.hender...@googlemail.com"
<cdm.hender...@googlemail.com> wrote:
> As I understand the standard, the following code will not perform as
> the developer expects and free memory of p1 & p2. MSVC8 confirms this
> and leaks 34bytes at p2. I do not get any compilation warnings, even
> at Level 4 (max warnings).
>
> int main(int, char**)
> {
>      char *p1 = new char[23];
>      char *p2 = new char[34];
>
>      delete[] p1,p2;
>      return 0;
>
> }
>
> I assume then, that the comma operator here is seen as the compiler as
> separating two statements; "delete[] p1" and "p2", and that "p2" as
> statement does nothing but is not an error. Is my understanding of
> this correct?

Yes except that they are expressions not statements.
IMHO The compiler should warn that p2 is an expression with no effect.

Best advice is never to use commas in C or C++ except to separate
arguments to functions (or expressions in for loops such as for
(i=0,j=42; i < 10; ++i,++j)).

Even better advice is that it is not generally exception safe to do
this sort of thing anyway. Consider replacing char with MyClass and
what happens if   new MyClass[34] throws an exception.

If you use vectors then you wont have the potential leaks.
If you had single objects instead of arrays then you should use
something like auto_ptr for the same reason.
With deletion handled by destructors there are no explicit calls to
separate with a comma.



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



