Topic: delete multiple pointers


Author: "cdm.henderson@googlemail.com" <cdm.henderson@googlemail.com>
Date: Sat, 5 Sep 2009 22:26:15 CST
Raw View
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?

Thanks
-- Craig

--
[ 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                      ]





Author: Ron <ron.natalie@gmail.com>
Date: Mon, 7 Sep 2009 02:21:58 CST
Raw View
On Sep 6, 12:26 am, "cdm.hender...@googlemail.com"
<cdm.hender...@googlemail.com> wrote:

>
>      delete[] p1,p2;

>
> 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?
>

It is a comma operator.  But what you have is two subexpressions
in one statement.   The first subexpression delete[] p1 is evaluated
and then it's value discarded and the right subexpression p2 is
evaluated and it becomes the value of the full expression (which
is ignored).

p2 is not deleted.



--
[ 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                      ]





Author: Nick Hounsome <nick.hounsome@googlemail.com>
Date: Mon, 7 Sep 2009 02:21:35 CST
Raw View
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                      ]