Skip to main content
gamedev.net
🔒 Locked

memory probs

Started by pIpEs ClaNGoR 2 replies 850+ views
Original Post
pIpEs ClaNGoR
pIpEs ClaNGoR
I am trying to improve this 3ds loader and have it find the data and allocate memory for it but there is a strange snag I have come across and can't figure it out.

#include "main.h"

struct vert
{
	float x,y,z;
};

struct face
{
	unsigned short a,b,c;
};

verts vertsinfo;

verts load3DS(char *mfile)
{
	FILE *modelfile;
	short chunk;
	int size;
	int totalsize;
	unsigned char name;

	unsigned short vertTotal;
	unsigned short vertcount;

	unsigned short faceTotal;
	unsigned short facecount;

	unsigned short textureTotal;

	int vertloop=0;
	int faceloop=0;
	int textureloop=0;

	if (!(modelfile=fopen(mfile, "rb")))
	{
		MessageBox(NULL,"Unable to load file!","ERROR",MB_OK|MB_ICONEXCLAMATION);
	}

	fread(&chunk, 2, 1, modelfile);
	fread(&totalsize, 4, 1, modelfile);

	if (chunk!=0x4d4d)
	{
		MessageBox(NULL,"Not a 3DS file!","ERROR",MB_OK|MB_ICONEXCLAMATION);
	}

	do
	{
		fread(&chunk, 2, 1, modelfile);
		fread(&size, 4, 1, modelfile);

		switch (chunk)
		{
			case 0x3d3d:
			break;

			case 0x4000:
				do fread(&name, 1, 1, modelfile);
				while (name != '\0');
			break;

			case 0x4100:
			break;

			case 0x4110:
				fread(&vertcount, 2, 1, modelfile);
				fseek(modelfile, 4*(vertcount*3), SEEK_CUR);

				vertTotal+=vertcount;
			break;

			case 0x4120:
				fread(&facecount, 2, 1, modelfile);
				fseek(modelfile, 2*(facecount*4), SEEK_CUR);

				faceTotal+=facecount;

			break;

			default:
				fseek(modelfile, size-6, SEEK_CUR);
		}

	}
	while(!(ftell(modelfile)==totalsize));

	vert *vertnumber=(vert *)malloc(vertTotal);
	face *facenumber=(face *)malloc(faceTotal);

	vertTotal=0;
	faceTotal=0;

	fseek(modelfile, 6, SEEK_SET);

	do
	{
		fread(&chunk, 2, 1, modelfile);
		fread(&size, 4, 1, modelfile);

		switch (chunk)
		{
			case 0x3d3d:
			break;

			case 0x4000:
				do fread(&name, 1, 1, modelfile);
				while (name != '\0');
			break;

			case 0x4100:
			break;

			case 0x4110:
				fread(&vertcount, 2, 1, modelfile);
				vertTotal+=vertcount;

				for(vertloop; vertloop<vertTotal; vertloop++)
				{
					fread(&vertnumber[vertloop].x, 4, 1, modelfile);
					fread(&vertnumber[vertloop].y, 4, 1, modelfile);
					fread(&vertnumber[vertloop].z, 4, 1, modelfile);
				}
			break;

			case 0x4120:
				fread(&facecount, 2, 1, modelfile);
				faceTotal+=facecount;

				for(faceloop; faceloop<faceTotal; faceloop++)
				{
					fread(&facenumber[faceloop].a, 2, 1, modelfile);
					fread(&facenumber[faceloop].b, 2, 1, modelfile);
					fread(&facenumber[faceloop].c, 2, 1, modelfile);
	
					fseek(modelfile, 2, SEEK_CUR);
				}

			break;

			default:
				fseek(modelfile, size-6, SEEK_CUR);
		}

	}
	while(!(ftell(modelfile)==totalsize));

	vertsinfo.x=(float *)malloc(faceTotal*3);
	vertsinfo.y=(float *)malloc(faceTotal*3);
	vertsinfo.z=(float *)malloc(faceTotal*3);

	vertsinfo.vertices=faceTotal*3;

	int pointnumber=0;
// freeing is fine but there is something going on in this loop in face loop 3.

	for (int loop=0; loop<faceTotal; loop++)
	{

		vertsinfo.x[pointnumber]=vertnumber[facenumber[0].a].x;
		vertsinfo.y[pointnumber]=vertnumber[facenumber[0].a].y;
		vertsinfo.z[pointnumber]=vertnumber[facenumber[0].a].z;

		pointnumber++;
		vertsinfo.x[pointnumber]=vertnumber[facenumber[0].b].x;
		vertsinfo.y[pointnumber]=vertnumber[facenumber[0].b].y;
		vertsinfo.z[pointnumber]=vertnumber[facenumber[0].b].z;

		pointnumber++;

		vertsinfo.x[pointnumber]=vertnumber[facenumber[0].c].x;
		vertsinfo.y[pointnumber]=vertnumber[facenumber[0].c].y;
		vertsinfo.z[pointnumber]=vertnumber[facenumber[0].c].z;

		pointnumber++;
	}


	fclose (modelfile);

	free(vertnumber);
	free(facenumber);

	return vertsinfo;
}

Something happens in the loop where it puts togeather the verts when pointnumber turns 3. So when I put numbers in vertsinfo.(x, y,and z)[3] and then free them I get an error at run time. I am stumped. It has to be something I am overlooking. [edited by - pipes clangor on April 19, 2004 9:40:27 AM]
---------------------------The pipes clangor all the time.
BriTeg
BriTeg
I didn''t go through your code too much, but shouldn''t this:


vert *vertnumber=(vert *)malloc(vertTotal);
face *facenumber=(face *)malloc(faceTotal);


be this?


vert *vertnumber=(vert *)malloc(vertTotal * sizeof(vert));
face *facenumber=(face *)malloc(faceTotal * sizeof(face));


Your other mallocs later on might need the same sort of changes. Malloc wants to know how many BYTES to allocate, not how many structs.

If I''m completely off (because I didn''t go through your code in detail), let me know and I will go through it in detail.
Brianmiserere nostri <font color=#BC
pIpEs ClaNGoR
pIpEs ClaNGoR
Ahhhhh man thank you it works now. You are right I though it gave you the number of how many variables of that array you wanted.
---------------------------The pipes clangor all the time.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.