PDA

View Full Version : glBuild2DMipmaps



Jameson Quave
05-24-2006, 04:40 AM
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

Jan
05-24-2006, 05:27 AM
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.

Mars_999
05-24-2006, 07:35 AM
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.

songho
05-24-2006, 08:31 AM
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].

jide
05-24-2006, 09:32 AM
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).

songho
05-24-2006, 02:01 PM
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]

Jan
05-24-2006, 02:10 PM
songho is right, using &cData[0] should work just fine.

jide
05-24-2006, 04:58 PM
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.

Jameson Quave
05-24-2006, 09:26 PM
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