Topic: a simple pointer question
Author: hkphoton@singnet.com.sg
Date: 2000/01/19 Raw View
#include <iostream>
class X
{
public:
~X();
};
X::~X()
{
cout << "X dest. called" << endl;
}
int main
{
X **a;
a = new X* [2];
a[0] = new X[2];
a[1] = new X[2];
delete [] a[0];
delete [] a[1];
delete [] a; // <------------ (1)
return (0);
}
in (1), can i use
delete a;
instead of
delete []a;
???
thx~
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Richard Parkin" <rparkin@msi-eu.com>
Date: 2000/01/19 Raw View
<hkphoton@singnet.com.sg> wrote in message
news:3885C6CA.C87C6451@singnet.com.sg...
>
> int main
> {
> X **a;
> a = new X* [2];
> a[0] = new X[2];
> a[1] = new X[2];
> delete [] a[0];
> delete [] a[1];
>
> delete [] a; // <------------ (1)
> return (0);
> }
>
> in (1), can i use
> delete a;
> instead of
> delete []a;
> ???
No. It can invoke undefined behaviour. You might get away with it,
especially with simple types, but in general no.
As a rule, if new has [], so must the corresponding delete.
Ric
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Dietmar Kuehl <dietmar.kuehl@claas-solutions.de>
Date: 2000/01/19 Raw View
Hi,
In article <3885C6CA.C87C6451@singnet.com.sg>,
hkphoton@singnet.com.sg wrote:
> int main
> {
> X **a;
> a = new X* [2];
> delete [] a; // <------------ (1)
> }
>
> in (1), can i use
> delete a;
> instead of
> delete []a;
No, you cannot: Every array object allocated with 'new' has to be
release using 'delete[]'. Using 'delete' instead would result in
undefined behavior. It does not matter whether the type is built-in, a
POD, or some class. In all cases you have to follow this rule.
--
<mailto:dietmar.kuehl@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>
Sent via Deja.com http://www.deja.com/
Before you buy.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/01/20 Raw View
hkphoton@singnet.com.sg wrote:
>
> #include <iostream>
>
> class X { ... };
>
> int main
> {
> X **a;
> a = new X* [2];
> a[0] = new X[2];
> a[1] = new X[2];
> delete [] a[0];
> delete [] a[1];
>
> delete [] a; // <------------ (1)
> return (0);
> }
>
> in (1), can i use
> delete a;
> instead of
> delete []a;
You must use the form of delete that matches the form of new.
Since 'a' was allocated with new[], it must be deallocated
with delete[]. The results of your program are otherwise undefined.
In particular, it is possible for new and new[] to use different
memory pools. In that case, 'delete a' would try to return the
storage to the wrong pool.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/01/20 Raw View
hkphoton@singnet.com.sg wrote:
>
>
> a = new X* [2];
> delete [] a; // <------------ (1)
> in (1), can i use
> delete a;
> instead of
> delete []a;
No. You must always use delete[] when you use new T[];
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]