|
From: Alison Z. <ali...@au...> - 2004-03-24 00:22:33
|
Hello All, I got the above error message when i run my program under valgrind. I do not quite understand what is wrong with my code: float **pts //defined in header file np = nPlanes; //nPlanes is a constructor prameter pts = new float*[np]; //this line is in constructor, it causes the above error Can anyone please tell me what is wrong here. Thanks a lot |
|
From: Alison Z. <ali...@au...> - 2004-03-24 01:38:26
|
Hi Avery, Thanks for reply. The 'entire' error is: ==1821== Address 0x44FA1BFC is 0 bytes inside a block of size 40 alloc'd ==1821== at 0x4002BD5C: __builtin_vec_new (vg_replace_malloc.c:203) ==1821== by 0x81508E9: CimGraphixLineSet::CimGraphixLineSet(int, Cim3DTransform &) (CimModel/CimGraphix/CimGraphixLineSet.C:241) ==1821== by 0x815A747: CimModelLVPS4x4::getModelImageIntersections(int, int, Cim3DVector *, Cim3DVector *) (CimModel/CimModel/CimModelLVPS4x4.C:1882) ==1821== by 0x816266F: CimModeller::getModelImageIntersections(int, int, int, Cim3DVector *, Cim3DVector *) (CimModel/CimModel/CimModeller.C:1050) np = nPlanes; //nPlanes is a constructor prameter pts = new float*[np]; those lines are in the constructor of CimGraphixLineSet cheers Alison |
|
From: Alison Z. <ali...@au...> - 2004-03-24 01:54:34
|
It is related to the previous error which is:
==1821== Mismatched free() / delete / delete []
==1821== at 0x4002BEE7: __builtin_delete (vg_replace_malloc.c:244)
==1821== by 0x81509B6: CimGraphixLineSet::~CimGraphixLineSet(void)
(CimModel/CimGraphix/CimGraphixLineSet.C:263)
this code is:
int pl;
for (pl=0;pl<np;pl++)
{
delete pts[pl];
}
delete pts; //in destructor
code in constructor: float **pts; pts = 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.
Thanks very much,
Alison
|
|
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-----
|
|
From: Alison Z. <ali...@au...> - 2004-03-24 21:12:58
|
Dear All, Thanks very much for your clear explaination. I have solved the problem now. Cheers Alison ------------------------------------------------- This mail sent through University of Auckland http://www.auckland.ac.nz/ |