|
From: Esben M. H. <es...@de...> - 2004-03-24 06:59:50
|
=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Wednesday 24 March 2004 02:50, Alison Zhang wrote:
> It is related to the previous error which is:
>
> =3D=3D1821=3D=3D Mismatched free() / delete / delete []
> =3D=3D1821=3D=3D at 0x4002BEE7: __builtin_delete (vg_replace_malloc.c:=
244)
> =3D=3D1821=3D=3D by 0x81509B6: CimGraphixLineSet::~CimGraphixLineSet(v=
oid)
> (CimModel/CimGraphix/CimGraphixLineSet.C:263)
>
> this code is:
> int pl;
>
> for (pl=3D0;pl<np;pl++)
> {
> delete pts[pl];
> }
> delete pts; //in destructor
> code in constructor: float **pts; pts =3D new float*[np];
> I quess the way i delete arrays is not correct, i am java programmer.
> Please give me some hint how to delete various kinds of arrays in c.
Having worked with several Java programmers in the past, I agree: you show =
all=20
the signs of Java->C++ conversion ;-) Several points:
1. You don't need or want to call new() every time you want to construct an=
=20
object. Use of new is association; if you want composition use=20
class myclass {
CompositeClass composition;
};
instead. Local variables should almost never be allocated with new.
2. Dynamic arrays are best handled with a helper object. The C++ standard=20
library (STL) and QT each comes with their own. These are std::vector (STL)=
,=20
QValueVector (QT) and QPtrVector (QT). I suggest you look into these.
3. To answer you question, here's a program that does the dynamic array thi=
ng:
#include <vector>
int main() {
int noelements =3D 5;
// creating an array
int* array =3D new int[noelements];
// deleting an array
delete[] array;
// it's better to use a vector
std::vector<int> better_array(5);
// vectors are objects, and thus takes care of their=20
// own cleanup during destruction.
return 0;
}
=2D --=20
regards, Esben
Homepage: http://www.mosehansen.dk
Signature fingerprint at http://www.mosehansen.dk/about
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQFAYTIvrfnftt13wXIRAiIAAJ9LxgZh/yKTyjxtbZkwp82dUyBNwgCeM7lT
=46ee+79aj0CGHStE1bbOtuDU=3D
=3DxKJy
=2D----END PGP SIGNATURE-----
|