Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

vector to gl*Pointer()

Started by Zakwayda Oct 8, 2004 at 7:42 AM 2 replies 1.3k views
Original Post
Zakwayda
Zakwayda
Quick question. Like most people I have various arrays that I create and send to OpenGL, such as: Vector3* m_verts; m_verts = new Vector3[m_numverts]; glVertexPointer(3, GL_FLOAT, VECTOR3_STRIDE, (float*)m_verts); delete [] m_verts; And so on. (Obviously not that code in that sequence, but those are the different things you would do with the array.) I have always assumed that it would be impossible to use std::vectors in this case because a) perhaps the internal vector memory isn't gauranteed to be contiguous and b) there's no easy way to access the internal data of the vector directly. So does anyone use std::vectors in cases like this? Is there a way to do it (safely, and without hacks)?
snk_kid
snk_kid
Quote:
Original post by jyk
I have always assumed that it would be impossible to use std::vectors in this case because a) perhaps the internal vector memory isn't gauranteed to be contiguous and


I'm most certain that the latest c++ standard i.e 2003 states that std::vector must be implementated with contigous memory, it wasn't the case before then all you could do was hope your implementation did it.

Quote:
Original post by jyk
b) there's no easy way to access the internal data of the vector directly.

So does anyone use std::vectors in cases like this? Is there a way to do it (safely, and without hacks)?


just take the address of the first element e.g.

#include <vector>struct foo {/* ... */};int main() {   std::vector<foo> f;   /* ...stuff... */   //later on...   foo* foo_ptr = &f[0]; // or this: foo* foo_ptr = &f.front(); is another way.   /* ...stuff... */      return 0;}


[Edited by - snk_kid on October 8, 2004 8:27:15 AM]
Enigma
Enigma
Yes, passing vectors to functions requiring raw C-style arrays by using &vector[0] is both safe and portable.

Enigma
Zakwayda
Zakwayda
Cool. It'll be nice to get rid of all those news and deletes :-)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.