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.
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).
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
But like I said, I doubt this is the thing that makes the second call to vertex allocation crash.
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:
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!!!
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
Originally posted by nukem:
[b]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!!![/b]
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:
there is some underlying problem that is not fixed but hidden by the use of new/delete instead of malloc/free; or, more likely,
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.