Advertisement

Error defining varible

Started by December 08, 2002 03:48 PM
2 comments, last by Nukem 22 years, 2 months ago
When I goto define a varible in a for loop the first time everything goes fine the second time around my program crashes with 'Segmentation fault' showing in konsole.
    
meshes[i].vertices = (Vertice*)malloc(sizeof(Vertice) * meshes[i].mesh.numvertexs * meshes[i].mesh.nummeshframes);
  
If you need to look at the hole funtion and the structs.
         
typedef struct Vertice
{
  signed short vec[3];
  unsigned char normal[2];
};

typedef struct Mesh
{
MeshHeader mesh;
Skin* skins;
Triangle* triangles;
TexCoord* texcoord;
Vertice* vertices; //this needs clearing out now


TEXTURE texture;
unsigned char settexture;
};

    bool LoadModel(char* FileName)
    {
      FILE* file;
      int i;

      file = fopen(FileName, "rb");

      if(!file)
      {
        fprintf(stderr, "Cannt find file %s!!!!\n", FileName);
        return false;
      }

      md3name = (char*)malloc(strlen(FileName));
      strcpy(md3name, FileName);

      fread(&header, 1, sizeof(Header), file);

      if((header.id[0] != 'I' || header.id[1] != 'D' || header.id[2] != 'P' || header.id[3] != '3') || header.version != 15)
      {
        fprintf(stderr, "Invalid file format not version 15 or ID not IDP3!!!\n");
        return 0;
      }

      boneframes = (BoneFrame*)malloc(sizeof(BoneFrame) * header.numboneframes);
      fread(boneframes, sizeof(BoneFrame), header.numboneframes, file);

      tags = (Tag*)malloc(sizeof(Tag) * header.numboneframes * header.numtags);
      fread(tags, sizeof(Tag), header.numboneframes * header.numtags, file);

      links = (NuclearMD3**)malloc(sizeof(NuclearMD3) * header.numtags);

      for(i = 0; i < header.numtags; i++)
      {
        links[i] = 0;
      }

      meshes = (Mesh*)malloc(sizeof(Mesh) * header.nummeshes);

      long meshoffset = ftell(file);

      for(i = 0; i < header.nummeshes; i++)
      {
        fseek(file, meshoffset, SEEK_SET);
        fread(&meshes[i].mesh, sizeof(MeshHeader), 1, file);

        fseek(file, meshoffset + meshes[i].mesh.tristart, SEEK_SET);
        meshes[i].triangles = (Triangle*)malloc(sizeof(Triangle) * meshes[i].mesh.numtriangles);
        fread(meshes[i].triangles, sizeof(Triangle), meshes[i].mesh.numtriangles, file);

        fseek(file, meshoffset + meshes[i].mesh.texvecstart, SEEK_SET);
        meshes[i].texcoord = (TexCoord*)malloc(sizeof(TexCoord*) * meshes[i].mesh.numvertexs);
        fread(meshes[i].texcoord, sizeof(TexCoord), meshes[i].mesh.numvertexs, file);

        fseek(file, meshoffset + meshes[i].mesh.vertexstart, SEEK_SET);
        meshes[i].vertices = (Vertice*)malloc(sizeof(Vertice) * meshes[i].mesh.numvertexs * meshes[i].mesh.nummeshframes); //the buggy code

        fread(meshes[i].vertices, sizeof(Vertice), meshes[i].mesh.nummeshframes * meshes[i].mesh.numvertexs, file);

        meshoffset += meshes[i].mesh.meshsize;
      }

      fclose(file);

      header.numboneframes -= 1;

      startframe = 0;
      endframe = header.numboneframes;

      return 1;
    }
              
If you want to just look at the hole class you can go here. [edited by - nukem on December 8, 2002 5:05:57 PM]
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
You need to make absolutely sure the header structs are packed properly. It looks like that the first mesh structure is being loaded fine, but the next one is slightly misaligned, giving wildly wrong values for things like numtriangles etc.
Advertisement
I dont think I follow you. What do you mean "the next one is misleading"?
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
Well this is interesting. I was in a C++ chat on IRC and I asked this question and one guy said ''Why are you using malloc? Its C++ right so why not use new?'' So I thought well I tryed everything else and I changed malloc to new and it worked!!! So I changed everything to new and in my destructor I changed free to delete [].

Well I got it working now thanks for all your help!!!
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind

This topic is closed to new replies.

Advertisement