PDA

View Full Version : Error defining Varible



nukem
12-07-2002, 02:15 PM
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.

Here is the code



meshes[i].vertices = (Vertice*)malloc(sizeof(Vertice) * meshes[i].mesh.numvertexs * meshes[i].mesh.nummeshframes);


Can someone please help me?

Thanks,
Nuke

Coriolis
12-07-2002, 02:22 PM
The use of the non-word "Vertice" is an abomination (I believe that is the official OpenGL stance, too). The singular form of "vertices" is "vertex".

Aside from that, there is nothing in that one line of code that looks bad in itself. I'd expect that 'i' is past the end of an array.

nukem
12-07-2002, 05:22 PM
Thats what I thought too. It looks like theres nothing wrong with it. Also Im a programmer not a writer. Im sorry if I cannt spell.

zeckensack
12-07-2002, 05:39 PM
This is a general c question methinks.

Anyway, I guess you defined meshes as a pointer to some struct.
Eg

struct mesh { <...> };

mesh* meshes;

Better use something like
mesh meshes[max_meshes];
You must insert a number there, mesh meshes[] won't cut it.

This will give you a fixed size array to fill. If you don't know the number of meshes at compile time, you can use


mesh* meshes=(mesh*)malloc(number_of_meshes*sizeof(mesh) );
The code snippet you posted is fine as far as I can see, and doesn't need to be changed.

nukem
12-07-2002, 06:34 PM
I do have that code to clear out meshes what needs clearing out is vertices. Thats why I have it doing it in the for loop.

Ill post the mesh and vertice structs for everyone.



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;
};

meshes[i].vertices = (Vertice*)malloc(sizeof(Vertice) * meshes[i].mesh.numvertexs * meshes[i].mesh.nummeshframes);


[This message has been edited by nukem (edited 12-07-2002).]

zeckensack
12-07-2002, 06:40 PM
Whoa, interesting vertex layout http://www.opengl.org/discussion_boards/ubb/biggrin.gif

But one piece is still missing. How do you declare your 'meshes' array?

nukem
12-07-2002, 06:47 PM
Its not an array its a pointer really.




Mesh* meshes;

zeckensack
12-07-2002, 06:56 PM
Alright then, I believe that's the problem. You don't have any storage allocated for meshes, just a pointer that points ... well nowhere.

Try
meshes=(Mesh*)malloc(number_of_meshes*sizeof(Mesh) );

number_of_meshes (or whatever you'd rather want to call it, you probably already have a name for it) should be the maximum i (the i from your first code snippet, the loop counter I guess).

Don't forget to free(meshes) when cleaning up.

nukem
12-07-2002, 07:07 PM
Ya I have that




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


Its something with vertices because I have all the other stuff in the mesh struct defined, vertices is last.

If you want ill post the hole funtion, if you think it will help.

Coriolis
12-07-2002, 09:53 PM
More code would be helpful.

nukem
12-07-2002, 10:10 PM
Here is the hole funtion.




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(&amp;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 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(&amp;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 troublesome code is here
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 look at the hole class go here (http://zonkbonk.virtualave.net/NuclearMD3.h)

[This message has been edited by nukem (edited 12-08-2002).]

Coriolis
12-07-2002, 11:23 PM
Well, I'm too tired right now to catch anything subtle. I am pretty sure this isn't your problem, but you malloc one too few characters for your string name. You need to malloc(strlen + 1) to make sure you have room for the '\0' terminator. This will cause random memory access failures because memory allocators usually give you a multiple of 32 bytes, or at the very least a multiple of 4 bytes, regardless of the size you actually ask for, meaning that most of the time you have a few characters you can write past the array. But the time you don't have those extra characters, you will cause bad things to happen http://www.opengl.org/discussion_boards/ubb/wink.gif

But like I said, I doubt this is the thing that makes the second call to vertex allocation crash.

nukem
12-07-2002, 11:32 PM
Ya I tryed + 100 and it did nothing. What do you mean by "this isnt your problem"?

jwatte
12-08-2002, 05:23 PM
Learning to debug is a very useful skill. Much more useful than any particular API (such as OpenGL). Evenso, this is an API discussion board, not a "how to debug" discussion board.

You didn't post what your types are. If sizeof(TexCoord) is greater than sizeof(TexCoord*) then these lines will give you trouble:




meshes[i].texcoord = (TexCoord*)malloc(sizeof(TexCoord*) * meshes[i].mesh.numvertexs);
fread(meshes[i].texcoord, sizeof(TexCoord), meshes[i].mesh.numvertexs, file);

nukem
12-08-2002, 05:54 PM
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!!!

OldMan
12-08-2002, 10:53 PM
Originally posted by Coriolis:
The use of the non-word "Vertice" is an abomination (I believe that is the official OpenGL stance, too). The singular form of "vertices" is "vertex".
.


He mar arguemnt that he was writing in portuguese...since vertice==vertex in portuguese http://www.opengl.org/discussion_boards/ubb/smile.gif

nukem
12-09-2002, 02:35 AM
Heh really? I had no idea. I do really suck in LA though, heh.

Coriolis
12-09-2002, 12:08 PM
Originally posted by nukem:
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!!!

If you use malloc and free together and new and delete together, this won't be a problem. If you try to delete memory you malloc or free memory you new, then the results are undefined (if I recall correctly).

However, the fact that using new fixed your problem means one of two things:
1) there is some underlying problem that is not fixed but hidden by the use of new/delete instead of malloc/free; or, more likely,
2) you were allocating the wrong size because of inaccurate sizeof statements, which seems more likely. I never do array = malloc(number * sizeof(type)) in my code, instead preferring array = malloc(number * sizeof(array[0]));. This means I can change the type of an element of array without having to find the two dozen places I implicitly assumed it was a short instead of an int, or vice versa. It also removes any possible confusion about whether or not a * should be used.

nukem
12-09-2002, 12:17 PM
ya it was probly the sizeof thing. I dont see why I used malloc Ive always used new and delete.

Thanks,
Nuke