Advertisement

Am i doing this right?

Started by May 18, 2002 12:33 PM
1 comment, last by llvllatrix 22 years, 9 months ago
Im new to memory manipulation (actually, im still in high scool), and i tried to write this code. But for some reason its screwing up. Anyone know why? Im sure its something stupid i cant see.
  
//Set asside memory for the character''s variables

	ColModel = (CollisionModel3D *)malloc(sizeof(CollisionModel3D) * nGroups);
	CollisionModel3D * ColTemp = ColModel;

	m_boneID = (char *)malloc(sizeof(char) * nGroups);
	char * boneTemp = m_boneID;

	g_names = (char *)malloc(sizeof(char [32]) * nGroups);
	char * nameTemp = g_names;

	n_groups = nGroups;		
	for (i = 0; i < (nGroups-1); i++)
	{
		pPtr += sizeof( byte );	// flags

		//Fill names with the respective group name

		nameTemp = new(char [32]);
		memcpy(nameTemp,pPtr,sizeof(char [32]));
		nameTemp += 32;
		pPtr += 32;				// name


		word nTriangles = *( word* )pPtr;
		pPtr += sizeof( word );
		int *pTriangleIndices = new int[nTriangles];

		//Create a new collisionmodel

		ColTemp = newCollisionModel3D();
		ColTemp->setTriangleNumber(nTriangles);

		//Loop Through All triangles

		for (int j = 0; j < (nTriangles-1); j++)
		{
			pTriangleIndices[j] = *( word* )pPtr;
			pPtr += sizeof( word );

			//Assign the joint id

			//Each group must have an individual boneID

			if (j == 0)
			{
				boneTemp = new(char);
				*boneTemp = m_pVertices[m_pTriangles[pTriangleIndices[0]].m_vertexIndices[0]].m_boneID;
				boneTemp++;
			}

			ColTemp->addTriangle(
			m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[0]].m_location,
			m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[1]].m_location,
			m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[2]].m_location);

			ColTemp->finalize();
		}
		//advance collision models

		ColTemp++;
		//for textures

		pPtr += sizeof( char );				
	}

	delete[] pBuffer;

	return true;
}
  
Thanks for any help.
It would help if you posted how it was 'screwing up' - ie is it compile or runtime. You are mixing malloc and new. Although they may well be implemented the same (new calling malloc) you still should not mix them.

#if 0 // Ignore this I tried it and am wrong, although stylistically I don't think new(char[32]) is very pretty
I don't think sizeof(char[32]) is ok (might be/might be not) try sizeof(char)*32

I don't think new(char[32]) is legal syntax, this should be

new char[32]
#endif

Also you don't seem to declare many of your variables, maybe these are in code we can't see but it makes it hard to know if your pointer manipulations are correct.

[edited by - JuNC on May 18, 2002 1:48:34 PM]
Advertisement
Does this help?


  //CHARACTER: represents models with a art element, collision element, and are animatedclass CHARACTER{public:	//Functions:	//Constructors	CHARACTER()	{		pModel = NULL;		pNext = NULL;	}	~CHARACTER()	{		if(!(pModel == NULL))			delete(pModel);		if(!(pNext == NULL))			delete(pNext);	}	//Used to load and initialize a character	bool InitCharacter(const char * modelFilename, 					   const char * collisionFilename);	//Returns a collision Model	CollisionModel3D * ReturnColModel(Matrix * FinalDisplacement, int & numgroups);	bool Draw(float r, float g, float b);		//Variables:	//Next Character	CHARACTER * pNext;private:	//Functions:	bool LoadCollisionModel( const char * filename);	//Variables		//The collision component	CollisionModel3D * ColModel;	//The m_boneIDs	char * m_boneID;	//The group names	char * g_names;	//number of groups	int n_groups;	//The art component	//Model Structure	Model * pModel;	};bool CHARACTER::LoadCollisionModel( const char *filename ){	//load and check file	ifstream inputFile( filename, ios::in | ios::binary | ios::nocreate );	if ( inputFile.fail())	{		return false;	// "Couldn''t open the model file."	}	char pathTemp[PATH_MAX+1];	int pathLength;	for ( pathLength = strlen( filename ); pathLength--; ) {		if ( filename[pathLength] == ''/'' || filename[pathLength] == ''\\'' ) {			break;		}	}	strncpy( pathTemp, filename, pathLength );	inputFile.seekg( 0, ios::end );	long fileSize = inputFile.tellg();	inputFile.seekg( 0, ios::beg );	byte *pBuffer = new byte[fileSize];	inputFile.read( pBuffer, fileSize );	inputFile.close();	const byte *pPtr = pBuffer;		//check milkshape header	MS3DHeader *pHeader = ( MS3DHeader* )pPtr;	pPtr += sizeof( MS3DHeader );	if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 )	{		return false; // "Not a valid Milkshape3D model file."	}	if ( pHeader->m_version < 3 || pHeader->m_version > 4 )	{		return false; // "Unhandled file version. Only Milkshape3D Version 1.3 and 1.4 is supported." );	}	int nVertices = *( word* )pPtr; 	int m_numVertices = nVertices;	VertexMS3D *m_pVertices = new VertexMS3D[nVertices];	pPtr += sizeof( word );		int i;	for ( i = 0; i < nVertices; i++ )	{		MS3DVertex *pVertex = ( MS3DVertex* )pPtr;		m_pVertices[i].m_boneID = pVertex->m_boneID;		memcpy( m_pVertices[i].m_location, pVertex->m_vertex, sizeof( float )*3 );		pPtr += sizeof( MS3DVertex );	}	int nTriangles = *( word* )pPtr;	int m_numTriangles = nTriangles;	TriangleMS3D *m_pTriangles = new TriangleMS3D[nTriangles];	pPtr += sizeof( word );	for ( i = 0; i < nTriangles; i++ )	{		MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr;		int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };		memcpy( m_pTriangles[i].m_vertexIndices, vertexIndices, sizeof( int )*3 );		pPtr += sizeof( MS3DTriangle );	}	int nGroups = *( word* )pPtr;	pPtr += sizeof( word );	//Set asside memory for the character''s variables	ColModel = (CollisionModel3D *)malloc(sizeof(CollisionModel3D) * nGroups);	CollisionModel3D * ColTemp = ColModel;	m_boneID = (char *)malloc(sizeof(char) * nGroups);	char * boneTemp = m_boneID;	g_names = (char *)malloc(sizeof(char [32]) * nGroups);	char * nameTemp = g_names;	n_groups = nGroups;			for (i = 0; i < (nGroups-1); i++)	{		pPtr += sizeof( byte );	// flags		//Fill names with the respective group name		//nameTemp = new char[32];		memcpy(nameTemp,pPtr,sizeof(char [32]));		nameTemp += 32;		pPtr += 32;				// name		word nTriangles = *( word* )pPtr;		pPtr += sizeof( word );		int *pTriangleIndices = new int[nTriangles];		//Create a new collisionmodel		//ColTemp = newCollisionModel3D();          //Unhandeled access violation here		ColTemp->setTriangleNumber(nTriangles);		//Loop Through All triangles		for (int j = 0; j < (nTriangles-1); j++)		{			pTriangleIndices[j] = *( word* )pPtr;			pPtr += sizeof( word );			//Assign the joint id			//Each group must have an individual boneID			if (j == 0)			{				//boneTemp = new char;				*boneTemp = m_pVertices[m_pTriangles[pTriangleIndices[0]].m_vertexIndices[0]].m_boneID;				boneTemp++;			}			ColTemp->addTriangle(			m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[0]].m_location,			m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[1]].m_location,			m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[2]].m_location);			ColTemp->finalize();		}		//advance collision models		ColTemp++;		//for textures		pPtr += sizeof( char );					}	delete[] pBuffer;	return true;}  


I think you were right about that new thing. When i tried to run it before, it gave an abnormal program termination. Now it just gives an illegal error operation (unhandeled access violation) where i indicated in the code. Im not sure why though.

Appreciate any help.

This topic is closed to new replies.

Advertisement