IceCoder
09-12-2007, 09:07
Ho un problema con malloc():
sto lavorando ad un progetto di grafica tridimensionale e devo caricare un modello 3D.
uso questa struttura:
#pragma pack(1)
typedef struct TAGcobHEADER
{
char Ident[3];
int nMeshes;
int nMaterials;
int FirstFrame;
int LastFrame;
int TicksPerFrame;
int FrameSpeed;
} cobHEADER;
typedef struct TAGcobVECTOR
{
float x;
float y;
float z;
} cobVECTOR;
typedef struct TAGcobMATERIAL
{
float ambR, ambG, ambB;
float diffR, diffG, diffB;
float specR, specG, specB;
float shine;
char tex[64];
} cobMATERIAL;
typedef struct TAGcobMESH
{
int nVertices;
int nFaces;
float posX, posY, posZ;
float rotX, rotY, rotZ;
float scalX, scalY, scalZ;
int useMaterial; //int as bool
int MatID;
cobVECTOR *VertexList;
int *FaceList;
} cobMESH;
#pragma pack()
class cobFILE
{
public:
cobHEADER Header;
cobMESH *MeshList;
cobMATERIAL *MaterialList;
int LoadModel(char *fname);
cobFILE();
cobFILE(char *fname);
~cobFILE();
};
Quando devo caricare la mesh in *MeshList creo un array di mesh ed in seguito per ogni mesh creo un array di vettori ed uno di 'int' per le facce.
Il problema è che dopo un totale di spazio allocato con malloc() ho un crash alla prima chiamata della medesima funzione...
Non capisco quale sia il problema... Perchè non posso creare molti puntatori?
Eppure lo spazio utilizzato dal modello di prova è realmente esiguo (poco meno di 320 bytes)
Posto di seguito la funzione per il caricamento del modello:
int cobFILE::LoadModel(char *fname)
{
FILE *f;
f = fopen(fname, "r+");
int meshes, mats;
int meshi = 0;
int mati = 0;
if(!f)
return 1; //FILE NOT FOUND
//READ HEADER
fread(&this->Header, sizeof(cobHEADER), 1, f);
if(this->Header.Ident[0] != 'C' || this->Header.Ident[1] != 'O' || this->Header.Ident[2] != 'B')
{
fclose(f);
return -1; //THIS IS NOT A VALID FORMAT
}
//ALLOCATE MEMORY
meshes = this->Header.nMeshes;
mats = this->Header.nMaterials;
if(meshes > 0)
{
this->MeshList = (cobMESH *)malloc(sizeof(cobMESH) * meshes);
//memset(this->MeshList, 0, sizeof(cobMESH) * meshes);
}
if(mats > 0)
{
this->MaterialList = (cobMATERIAL *)malloc(sizeof(cobMATERIAL) * mats);
//memset(this->MaterialList, 0, sizeof(cobMATERIAL) * mats);
}
//READ MODEL
while(!feof(f))
{
char chunkID = 0;
fread(&chunkID, sizeof(char), 1, f);
switch(chunkID)
{
case cob_MESH: //IT IS A MODEL!
{
int i;
cobMESH *msh = (cobMESH *)((long)this->MeshList + (long)(sizeof(cobMESH) * meshi));
if(msh == NULL)
{
fclose(f);
return -2; //ERROR IN ALLOCATING MEMORY STORAGE
}
msh->FaceList = (int *)malloc(sizeof(int) * msh->nFaces * 3);
msh->VertexList = (cobVECTOR *)malloc(sizeof(cobVECTOR) * msh->nVertices);
fread(&msh->nVertices, sizeof(int), 1, f);
fread(&msh->nFaces, sizeof(int), 1, f);
fread(&msh->posX, sizeof(float), 1, f);
fread(&msh->posY, sizeof(float), 1, f);
fread(&msh->posZ, sizeof(float), 1, f);
fread(&msh->rotX, sizeof(float), 1, f);
fread(&msh->rotY, sizeof(float), 1, f);
fread(&msh->rotZ, sizeof(float), 1, f);
fread(&msh->scalX, sizeof(float), 1, f);
fread(&msh->scalY, sizeof(float), 1, f);
fread(&msh->scalZ, sizeof(float), 1, f);
fread(&msh->useMaterial, sizeof(int), 1, f);
fread(&msh->MatID, sizeof(int), 1, f);
for(i = 0; i < msh->nVertices; i++)
{
cobVECTOR *v;
float x, y, z;
fread(&x, sizeof(float), 1, f);
fread(&y, sizeof(float), 1, f);
fread(&z, sizeof(float), 1, f);
v = (cobVECTOR *)((long)msh->VertexList + (long)(sizeof(cobVECTOR) * i));
v->x = x;
v->y = y;
v->z = z;
_asm{ nop }
}
for(i = 0; i < msh->nFaces * 3; i++)
{
int *v;
int x;
fread(&x, sizeof(int), 1, f);
v = (int *)((long)msh->FaceList + (long)(sizeof(int) * i));
*(v) = x;
_asm{ nop }
}
}
}
}
fclose(f);
return 0; //MODEL LOADED
}
sto lavorando ad un progetto di grafica tridimensionale e devo caricare un modello 3D.
uso questa struttura:
#pragma pack(1)
typedef struct TAGcobHEADER
{
char Ident[3];
int nMeshes;
int nMaterials;
int FirstFrame;
int LastFrame;
int TicksPerFrame;
int FrameSpeed;
} cobHEADER;
typedef struct TAGcobVECTOR
{
float x;
float y;
float z;
} cobVECTOR;
typedef struct TAGcobMATERIAL
{
float ambR, ambG, ambB;
float diffR, diffG, diffB;
float specR, specG, specB;
float shine;
char tex[64];
} cobMATERIAL;
typedef struct TAGcobMESH
{
int nVertices;
int nFaces;
float posX, posY, posZ;
float rotX, rotY, rotZ;
float scalX, scalY, scalZ;
int useMaterial; //int as bool
int MatID;
cobVECTOR *VertexList;
int *FaceList;
} cobMESH;
#pragma pack()
class cobFILE
{
public:
cobHEADER Header;
cobMESH *MeshList;
cobMATERIAL *MaterialList;
int LoadModel(char *fname);
cobFILE();
cobFILE(char *fname);
~cobFILE();
};
Quando devo caricare la mesh in *MeshList creo un array di mesh ed in seguito per ogni mesh creo un array di vettori ed uno di 'int' per le facce.
Il problema è che dopo un totale di spazio allocato con malloc() ho un crash alla prima chiamata della medesima funzione...
Non capisco quale sia il problema... Perchè non posso creare molti puntatori?
Eppure lo spazio utilizzato dal modello di prova è realmente esiguo (poco meno di 320 bytes)
Posto di seguito la funzione per il caricamento del modello:
int cobFILE::LoadModel(char *fname)
{
FILE *f;
f = fopen(fname, "r+");
int meshes, mats;
int meshi = 0;
int mati = 0;
if(!f)
return 1; //FILE NOT FOUND
//READ HEADER
fread(&this->Header, sizeof(cobHEADER), 1, f);
if(this->Header.Ident[0] != 'C' || this->Header.Ident[1] != 'O' || this->Header.Ident[2] != 'B')
{
fclose(f);
return -1; //THIS IS NOT A VALID FORMAT
}
//ALLOCATE MEMORY
meshes = this->Header.nMeshes;
mats = this->Header.nMaterials;
if(meshes > 0)
{
this->MeshList = (cobMESH *)malloc(sizeof(cobMESH) * meshes);
//memset(this->MeshList, 0, sizeof(cobMESH) * meshes);
}
if(mats > 0)
{
this->MaterialList = (cobMATERIAL *)malloc(sizeof(cobMATERIAL) * mats);
//memset(this->MaterialList, 0, sizeof(cobMATERIAL) * mats);
}
//READ MODEL
while(!feof(f))
{
char chunkID = 0;
fread(&chunkID, sizeof(char), 1, f);
switch(chunkID)
{
case cob_MESH: //IT IS A MODEL!
{
int i;
cobMESH *msh = (cobMESH *)((long)this->MeshList + (long)(sizeof(cobMESH) * meshi));
if(msh == NULL)
{
fclose(f);
return -2; //ERROR IN ALLOCATING MEMORY STORAGE
}
msh->FaceList = (int *)malloc(sizeof(int) * msh->nFaces * 3);
msh->VertexList = (cobVECTOR *)malloc(sizeof(cobVECTOR) * msh->nVertices);
fread(&msh->nVertices, sizeof(int), 1, f);
fread(&msh->nFaces, sizeof(int), 1, f);
fread(&msh->posX, sizeof(float), 1, f);
fread(&msh->posY, sizeof(float), 1, f);
fread(&msh->posZ, sizeof(float), 1, f);
fread(&msh->rotX, sizeof(float), 1, f);
fread(&msh->rotY, sizeof(float), 1, f);
fread(&msh->rotZ, sizeof(float), 1, f);
fread(&msh->scalX, sizeof(float), 1, f);
fread(&msh->scalY, sizeof(float), 1, f);
fread(&msh->scalZ, sizeof(float), 1, f);
fread(&msh->useMaterial, sizeof(int), 1, f);
fread(&msh->MatID, sizeof(int), 1, f);
for(i = 0; i < msh->nVertices; i++)
{
cobVECTOR *v;
float x, y, z;
fread(&x, sizeof(float), 1, f);
fread(&y, sizeof(float), 1, f);
fread(&z, sizeof(float), 1, f);
v = (cobVECTOR *)((long)msh->VertexList + (long)(sizeof(cobVECTOR) * i));
v->x = x;
v->y = y;
v->z = z;
_asm{ nop }
}
for(i = 0; i < msh->nFaces * 3; i++)
{
int *v;
int x;
fread(&x, sizeof(int), 1, f);
v = (int *)((long)msh->FaceList + (long)(sizeof(int) * i));
*(v) = x;
_asm{ nop }
}
}
}
}
fclose(f);
return 0; //MODEL LOADED
}