displaying array of vertexs

I’m trying to displaying a 3ds file. I can read all the vertices just fine and I have them stored in a C++ vector. But when I try to display them using a glDrawArray (I think this is the one) I get a seg. fault. I’m not sure where my problem is, and I’m really not sure on the correct way to display these vertices. Can any one help me out here?


Seg faults are usually caused by touching memory you have no right to touch. If the error appears to be in yourf glDrawArray call, you are probably telling glDrawArray to access an element in your array that is beyond the bounds of that array.

Yes that’s what i was thinking also. I was using the vectors’ size() function to get the number of elements, and I had also tried a small portion of vertices to see if it was the problem. I have no problem simply going through the vector and displaying the values. Using the same values that I was passing to glDrawArrays.
I don’t know where my problem is. :frowning:

This is what I have going on I have 501 vertices each with 3 points. So I have a C++ stl vector with 501*3 elements. So then I go

I can’t remember the exacts of the code but it’s something close to that. I wanted to see if I was accessing something out of my vector so I did this:
for(i = 0; i < list.size(); i++)
", list[i]);
and it worked fine. Any one got any ideas?

[This message has been edited by killercow (edited 08-06-2002).]

Maybe it would be helpful if you post the line of code you use for glDrawArrays, as well as how you create your array. If you are dynamically creating a multi-dimensional array you can run into problems as well, because the memory is not contiguous.

I don’t have my code here at work but I do remember how I created the array. Since I’m using a c++ vector I just find out the number of elements i need, 501 vertices so I need 501*3 then

then I set each element to the values i’m getting from the file.
How would you handle this?

[This message has been edited by killercow (edited 08-06-2002).]

i’d maybe use a simple array instead of c++ vectors for this. i’m not too sure on the interior workings of the stl vector. is the vector continuous in memory? i know when you create a vector, a certain amount of memory is allocated. once you push_back enough times to fill up that space, do the existing entries in the vector get copied to a bigger memory space? or are the new entries just referenced? not sure…that’s why i would use arrays.


Alright thanks for the help guys. I’ll try simple arrays. I think I was more worried about whether or not I was actually calling the right functions. For example in glDrawArrays does it matter if the array is a single array, or does it have to be multi-dimenisonal array (#vertices,3)?

An stl vector can occasionally re-allocate memory. So for instance if you do a push_back and it needs to grow the size of the vector something like this goes on…

float *pNew = new float[new size];

memcpy(pOld, pNew, sizeof(float)*pOld.count());

delete [] pOld;
pOld = pNew;

So… if you are pushing new stuff onto there and not re-setting the array that glDrawArrays uses you could be running into a dangling pointer situation.

So far as single vs multi-dimensional array… A single array is generally best. Multi-dimensional arrays that have a static size are contiguous in memory, but that’s not the case when you dynamically allocate them, since you need to allocate individual dimensions.

Ok well i’m gonna try it with a simple GL_float array. I’m not quite sure why I’m using vectors, but oh well…
Thanks for all the help guys