C++ array always has the same values for each element

Recommended Posts

gchris6810    207

Hi,

i'm sure there is an easy answer to this but I am finding it particularly difficult. I want to store mesh data in a static array of C++ classes but when I allocate to each element in the array it sets all the elements in the array with the same value. I initialize the array as follows:

mMeshes = new AnimableMesh[mNumMeshes];

.........

for( int i = 0; i < mNumMeshes; i++ )
{
mMeshes[i].SetMaterial(materialName); // this sets the material but all elements are set to this value
.....
}


Normally I would use the C malloc function which works fine but it isn't ideal for class initialization.

EDIT: I declare the class array as an instance variable in the class definition.

Thanks.

Edited by gchris6810

Share on other sites
gchris6810    207

No, sorry, that was just example code. in the actual code it uses a variable read in from the file. I have validated that each element read from the file is unique.

Share on other sites
gchris6810    207

OK here it is, but I though it would be easier if you had a more concise representation.

mMeshes = new AnimableMesh[mNumMeshes];

file->Seek( mJointOfs, File::IO_SEEK_SET );

mBindPose = new AnimableMesh::Joint[mNumJoints];

/* Load the armature's bind pose position and orientation */

for( int i = 0; i < mNumJoints; i++ )
{

mBindPose[i].orient.ComputeW( );
}

/* Load the mesh data */

file->Seek( mMeshesOfs, File::IO_SEEK_SET );

for( int meshIndex = 0; meshIndex < mNumMeshes; meshIndex++ )
{
char material[64];
mMeshes[meshIndex].SetMaterialName( material );
/* Allocate all the mesh data */

for( int i = 0; i < mMeshes[meshIndex].GetNumVertices( ); i++ )
{
AnimableMesh::Vertex *vertex = mMeshes[meshIndex].GetVertexPtr( i );
Math::DrawVert *drawVert = mMeshes[meshIndex].GetDrawVertPtr( i );

}

for( int i = 0; i < mMeshes[meshIndex].GetNumIndices( ); i++ )
{

short *index1 = mMeshes[meshIndex].GetIndexPtr( i*3 );
short *index2 = mMeshes[meshIndex].GetIndexPtr( i*3+1 );
short *index3 = mMeshes[meshIndex].GetIndexPtr( i*3+2 );

}

for( int i = 0; i < mMeshes[meshIndex].GetNumWeights( ); i++ )
{

AnimableMesh::Weight* weight = mMeshes[meshIndex].GetWeightPtr( i );

}

PrepareMeshes( mBindPose );

mMeshes[meshIndex].CreateBuffers( );
}


Share on other sites
gchris6810    207

Thanks. It works I just used strcpy.  For future reference what was it that made all the objects in the array set to the same value?

Share on other sites

The pointers were all pointing at the same memory location. It will have been set to the value of the last thing you processed.

Share on other sites
kunos    2254

Thanks. It works I just used strcpy.  For future reference what was it that made all the objects in the array set to the same value?

the lesson to bring home here is: if you are working with C++, USE C++ and forget about C.

The code you have posted is bound to fail in subtle ways (as you have discovered). Unless you have very good reasons not to, ALWAYS use std::string instead of char* and ALWAYS use std::vector instead of old style arrays... the code will end up being easier to read for you, for people trying to help you, for people you work with and it will also be safer.

Edited by kunos