Problems with using a Long as a loop index

This topic is 3790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi there, I've got the header of my model file loading fine, from what I can tell it's loading without any problems. The next step is to the load the actual model data. The problem I'm having is that the number of vertices is stored as a long like so: newObject->hVertices. Now the problem I'm having is when I try and use it in a for loop, it crashes out the program for (long t = 0; t < 12; t++) { tempBuffer = (float*) malloc (sizeof(float)); fread(tempBuffer,1,sizeof(float),model); fileData.oTVertices.push_back(*tempBuffer); } That works fine. However, if I do this for (long t = 0; t < newObject->hVertices; t++) { tempBuffer = (float*) malloc (sizeof(float)); fread(tempBuffer,1,sizeof(float),model); fileData.oTVertices.push_back(*tempBuffer); } That crashes out. Any suggestions? Cheers

Share on other sites
The inside of the loop is the same so it's probably here

newObject->hVertices

dereferencing a null pointer?

Share on other sites
How do you mean?

This is how I'm populating my structures, and the structures themselves.

[source lang=cpp[struct CMF_FILE_HEADER {	unsigned char	fVersion;	unsigned char	fNumObjs;};struct CMF_OBJECT_HEADER {	unsigned long	hVertices;	unsigned short	hFaces;	unsigned long	hTVertices;};CMF_FILE_HEADER * newFile;CMF_OBJECT_HEADER * newObject;newFile = (CMF_FILE_HEADER*) malloc (sizeof(CMF_FILE_HEADER));newObject = (CMF_OBJECT_HEADER*) malloc (sizeof(CMF_OBJECT_HEADER));[/source[

Share on other sites
Quote:
What tells you that?
Quote:
 The next step is to the load the actual model data. The problem I'm having is that the number of vertices is stored as a long like so: newObject->hVertices.
I see no problem there...
Quote:
 Now the problem I'm having is when I try and use it in a for loop, it crashes out the program
Ah. What happens when you try to use it elsewhere? Do you make it into the loop at all, and if so, how many times?
Quote:
 for (long t = 0; t < 12; t++) { tempBuffer = (float*) malloc (sizeof(float)); fread(tempBuffer,1,sizeof(float),model); fileData.oTVertices.push_back(*tempBuffer); }

There's no need to dynamically allocate all this every time through the loop if you're just storing the value:
float temp;for (long t = 0; t < 12; t++){  fread(&temp, 1, sizeof(float), model);  fileData.oTVertices.push_back(temp);}
This'll get rid of some potential errors (and a memory leak), as well as speed things up for models with lots of vertices. As mentioned above, though, the error probably lies elsewhere. What does your debugger say? Is newObject a valid pointer? What's the value of newObject->hVertices? Are there that many vertices in the model?

Quote:
 This is how I'm populating my structures, and the structures themselves.
I don't see any populating of structures. Where do you actually fill the fields with data from the file?

-jouley

Share on other sites
[source lang=cpp]// Read the File Headerfread (newFile,1,sizeof(CMF_FILE_HEADER),model);// Read the Object Headerfread (newObject,1,sizeof(CMF_OBJECT_HEADER),model);

Apologies, forgot to copy and paste that bit.

The reason I'm pretty sure that the headers are loading correctly is because i can use sprintf() and MessageBox() to get it to output the version number and it appears correctly.

Share on other sites
[source lang=cpp]using namespace std;struct VERTEX {	float	x;	float	y;	float	z;};struct FACE {	unsigned short	a;	unsigned short	b;	unsigned short	c;};struct CMF_FILE_HEADER {	unsigned char	fVersion;	unsigned char	fNumObjs;};struct CMF_OBJECT_HEADER {	unsigned long	hVertices;	unsigned short	hFaces;	unsigned long	hTVertices;};long loadModel(char* path){	FILE *model;	if((model=fopen(path,"r"))==NULL)	{		writeLog("Model Loading","The Model File Specified Does Not Exist",FALSE);	} else {		CMF_FILE_HEADER * newFile;		CMF_OBJECT_HEADER * newObject;		newFile = (CMF_FILE_HEADER*) malloc (sizeof(CMF_FILE_HEADER));		newObject = (CMF_OBJECT_HEADER*) malloc (sizeof(CMF_OBJECT_HEADER));					// Read the File Header		fread (newFile,1,sizeof(CMF_FILE_HEADER),model);		// Read the Object Header		fread (newObject,1,sizeof(CMF_OBJECT_HEADER),model);		// Set the arrays		struct modelData {			vector<float>	oVertices;			vector<float>	oTVertices;			vector<float>	oNormals;			vector<short>	oFaces;		} fileData;				// Read Vertex Data		float * tempBuffer;		tempBuffer = (float*) malloc (sizeof(float));			for (unsigned long v = 0; v < newObject->hVertices; v++)		{			fread(tempBuffer,1,sizeof(float),model);			fileData.oVertices.push_back(*tempBuffer);			fileData.oVertices.push_back(*tempBuffer);			fileData.oVertices.push_back(*tempBuffer);		}		for (unsigned long v = 0; v < newObject->hTVertices; v++)		{			fread(tempBuffer,1,sizeof(float),model);			fileData.oTVertices.push_back(*tempBuffer);			fileData.oTVertices.push_back(*tempBuffer);			fileData.oTVertices.push_back(*tempBuffer);		}		for (unsigned short v = 0; v < newObject->hFaces; v++)		{			fread(tempBuffer,1,sizeof(short),model);			fileData.oFaces.push_back(*tempBuffer);			fileData.oFaces.push_back(*tempBuffer);			fileData.oFaces.push_back(*tempBuffer);		}		writeLog("Model Loading","",TRUE);	}	fclose(model);	return 0;}

For completeness sake I have included the entire module code.

I've been able to narrow the problem down to the TVertices loop. Any suggestions?

Cheers for your help thus far

Share on other sites
Quote:
 Original post by AndyEsserI've been able to narrow the problem down to the TVertices loop. Any suggestions?
Yes: stop wasting your time with MessageBox and whatnot, and learn to use your debugger.

Share on other sites
If you are going to be using some features of C++, you may as well use them all and avoid the old C style code, which is already obsolete.

Here is what I would do with the code, taking some guesses as to what you are actually trying to do.

#include <string>#include <iostream>struct Vertex{    float X;    float Y;    float Z;};struct Face{    int A;    int B;    int C;};struct CmfFileHeader{    int Version;    int ObjectCount;};struct CmfObjectHeader{    long VertexCount;    long TVertexCount;    long FaceCount;    std::vector<Vertex> Vertices;    std::vector<Vertex> TVertices;    std::vector<Face> Faces;};class CmfModel{public:    CmfModel(std::string path);    CmfFileHeader FileHeader;    std::vector<CmfObjectHeader> Objects;};CmfModel::CmfModel(std::string path){    std::ifstream file(path.c_str());    file >> FileHeader.Version;    file >> FileHeader.ObjectCount;    for (int i = 0; i < FileHeader.ObjectCount; i++)    {        CmfObjectHeader object;        file >> object.VertexCount;        file >> object.TVertexCount;        file >> object.FaceCount;        for (int j = 0; j < object.VertexCount; j++)        {            Vertex vertex;            file >> vertex.X;            file >> vertex.Y;            file >> vertex.Z;            object.Vertices.push_back(vertex);        }              for (int j = 0; j < object.TVertexCount; j++)        {            Vertex vertex;            file >> vertex.X;            file >> vertex.Y;            file >> vertex.Z;            object.TVertices.push_back(vertex);        }         for (int j = 0; j < object.FaceCount; j++)        {            Face face;            file >> face.A;            file >> face.B;            file >> face.C;            object.Faces.push_back(face);        }                Objects.push_back(object);    }}

Share on other sites
Thank you very much for that, but MSVC++ is having problems with this line

std::ifstream file(path.c_str());

Any suggestions?

Share on other sites
Quote:
 Original post by AndyEsserThank you very much for that, but MSVC++ is having problems with this linestd::ifstream file(path.c_str());Any suggestions?

That you tell us what specific "problems" it is having.

• 37
• 12
• 10
• 10
• 9
• Forum Statistics

• Total Topics
631362
• Total Posts
2999571
×