Jump to content
  • Advertisement
Sign in to follow this  
Lord_Vader

Can anyone help me with this code?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

The problem is that the fread funcion doesnt read the file when I try to dynamically allocate the memory(it throws exceptions): I have a class, with some pointers for example
.
.//In the class
.
private:
polygon_class* polygons;
.
.
.

and a function in that class like:
.
.
.
//Code within a classes function

		case 0x4120:
				fread (&numofP, sizeof (unsigned short), 1, l_file);
                this->num0fpolygons = numofP;

				polygons = new polygon_class[numofP];

					fread (&polygon.a, 2, 1, fileP);
					fread (&polygons.b, 2, 1, fileP);
					fread (&polygons.c, 2, 1, fileP);
					fread (&flags, 2, 1, fileP);
	
				}
                break;
.
.
.

the polygon class is:
class polygon_class
{
public:
	unsigned int a,b,c;
};
when I try the same using arrays instead of the pointer it works! any ideas?I am desperate

Share this post


Link to post
Share on other sites
Advertisement

fread (&polygon.a, 2, 1, fileP);
fread (&polygons.b, 2, 1, fileP);
fread (&polygons.c, 2, 1, fileP);

What is i, and what is polygon?

That code shouldn't even compile, you've got a random closing brace. That brace probably refers to the for loop you didn't bother posting. If you want help finding bugs in your code, please post your code. Not a few random lines that don't make any sense.

CM

Share this post


Link to post
Share on other sites
Just post the complete code, that's the only way to see the program in its context.

EDIT: ehh yeah, what he said :)

Share this post


Link to post
Share on other sites
I am sorry
Here is the whole function:


char Model::LoadModel(char *filename)
{
int i;

FILE *file;

unsigned short chunk_id;
unsigned int chunk_length;

unsigned char char;
unsigned short howmany = 0;
unsigned short flags = 0;

if (fopen_s(&file,filename, "rb"))
return 0;

while (ftell(file) < _filelength (_fileno (file)))
{

fread (&chunk_id, 2, 1, file);
fread (&chunk_length, 4, 1, file);

switch (chunk_id)
{

case 0x4d4d:
break;
case 0x3d3d:
break;
case 0x4000:
i=0;
do
{
fread (&char, 1, 1, file);
this->name=char;
i++;
}while(char != '\0' && i<20);
break;
case 0x4100:
break;

case 0x4110:
fread (&howmany, sizeof (unsigned short), 1, file);
this->vertices_qty = howmany;

vertex = new vertex_class[vertices_qty];
{
fread (&vertex.x, sizeof(float), 1, file);
fread (&vertex.y, sizeof(float), 1, file);
fread (&vertex.z, sizeof(float), 1, file);
}
break;


case 0x4120:
fread (&howmany, sizeof (unsigned short), 1, file);
this->polygons_qty = howmany;

polygons = new polygon_class[howmany];

for (i=0; i<howmany; i++)
{
fread (&polygons.a, 2, 1, file);
fread (&polygons.b, 2, 1, file);
fread (&polygons.c, 2, 1, file);
fread (&flags, 2, 1, file);
}
break;

case 0x4140:
fread (&howmany, sizeof (unsigned short), 1, file);

mapcoord = new mapcoord_class[howmany];

for (i=0; i<howmany; i++)
{
fread (&mapcoord.u, sizeof (float), 1, file);
fread (&mapcoord.v, sizeof (float), 1, file);
}
break;
}
}
fclose (file);
return (1);
}







It's a .3ds loader
It loads normally the vertexes but when it comes to polygons at run time it throws:

Unhandled exception at 0x00462840 in main.exe: 0xC0000005: Access violation reading location 0x00000004.

I cant find why and I dont understand why provided that it reads normally the vertexes,
it stalls when reading the polygons

The model class has private pointers:

vertex_class* vertex;
polygon_class* polygon;
mapcoord_class* mapcoord;

where:

class vertex_class
{
public:
float x,y,z;
};

class polygon_class
{
public:
unsigned int a,b,c;
};

class mapcoord_class
{
public:
float u, v;
};







thanks

[Edited by - Lord_Vader on September 9, 2006 2:43:53 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Quote:


polygon = new polygon_class[howmany];

fread (&polygons.a, 2, 1, file);



Nope,It was a mistake while copying and pasting the code,
the problem persists

Share this post


Link to post
Share on other sites
Yes, I have used fopen_s(another transfering mistake) look again


Quote:
Original post by ikslm
fread (&polygon.a, 2, 1, fileP);

sizeof(unsigned int) isn't 2 but 4 bytes.


So?
When I replace the pointer in the class:
polygon_class* polygons; with an array:

polygon_class polygons[20000];

everything runs perfectly
What do you suggest?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!