glBuild2DMipmaps

I’ve been having a problem with my program crashing, I know this isn’t 100% an opengl problem… but maybe someone could offer some suggestions? It’s crashing were marked…
code here:
http://rafb.net/paste/results/NOVfUe55.html

You are passing a pointer to a vector to gluBuild2DMipmaps.

A vector is not the same as a typical C-Array, no wonder it crashes.

Look at some tutorials, to see how to do it right:
nehe.gamedev.net

Jan.

Originally posted by Jameson Quave:
I’ve been having a problem with my program crashing, I know this isn’t 100% an opengl problem… but maybe someone could offer some suggestions? It’s crashing were marked…
code here:
http://rafb.net/paste/results/NOVfUe55.html

If you are going to use STL vectors you need to use them as a C array/pointer for OpenGL to use it.

I think the crash came from the second last line:
free( &cData );

You cannot deallocate the vector with free(). (It must be a pair, malloc-free.) Indeed, it will be automatically destructed when the function exits. Just remove the above line.

And, since C++ vector class is also a contiguous memory, you can use it as C-style array in a way. The last param of gluBuild2DMipmaps() would be:
&cData[0].

No songho. vector is a class that can have more than a single member inside it. If this is the case (and it is implementation dependant to do so), he simply cannot do it like that.
More, nothing, from what I know at least, can say all the elements in vector are contiguous in memory, it depends on the type of allocator, so doing &(*cData.begin()) wouldn’t be of help neither.

But you are right for free (&cData).

jide,
I don’t quite follow what you meant. C++ vector class is a data structure with contiguous memory location. All elements in the vector container are stored one after the other. So, you can directly access any elements via the subscript operator [] same as conventional array does.

My point was: use the address where the data actually starts from, not the address of the vector object.

I reviewed Jameson Quave’s codes, and found out he stores struct variables(ColorRGB) into the vector container. Eek! I missed this part. I thought he stores GLubyte data into the vector. However, &cData[0] is still okay in memory-wise, because his struct has only 3 members (its size is 3 bytes).

Although, it must be casted to (GLubyte*). The compiler should stop at that point and notify the type-conversion error.

It is a bad casting, but it should work with:
static_cast<GLubyte*>&cData[0]

songho is right, using &cData[0] should work just fine.

Yes you’re right. I was persuaded nothing could tell if all elements of a std::vector are contiguous or not in memory. Anyway &cData[0] is the right form but not &cData.

well i went another route since many coders disagreed with the idea of using vector’s for this, and just used a few simple arrays… it was simpler and turned out to work… thanks for the advice