Problems with using a Long as a loop index
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
The inside of the loop is the same so it's probably here
newObject->hVertices
dereferencing a null pointer?
newObject->hVertices
dereferencing a null pointer?
How do you mean?
This is how I'm populating my structures, and the structures themselves.
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[
Quote:Original post by AndyEsserWhat tells you that?
Hi there,
I've got the header of my model file loading fine, from what I can tell it's loading without any problems.
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 programAh. 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
[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.
[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
Quote:Original post by AndyEsserYes: stop wasting your time with MessageBox and whatnot, and learn to use your debugger.
I've been able to narrow the problem down to the TVertices loop. Any suggestions?
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.
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); }}
Thank you very much for that, but MSVC++ is having problems with this line
std::ifstream file(path.c_str());
Any suggestions?
std::ifstream file(path.c_str());
Any suggestions?
Quote:Original post by AndyEsser
Thank you very much for that, but MSVC++ is having problems with this line
std::ifstream file(path.c_str());
Any suggestions?
That you tell us what specific "problems" it is having.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement