Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Lev Povalahev

OT: should I?

This topic is 5987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Everybody uses STL nowadays. I''m not very familiar with it, but after reading the C++ FAQ and some STL tutorials I realize that in some situations STL can save some development time. I have a question: can I use STL''s vector for example as a containter for my vertex arrays and pass it directly to glVertexPointer? It is guaranteed to be continuous memory?? Are there any problems using STL with OpenGL? I''ve read a lot of threads on this subject(usenet), but almost every time a flame started. Please don''t start the flame here and sorry for an OT question. Regards, -Lev

Share this post


Link to post
Share on other sites
Advertisement
>> can I use STL''s vector for example as a containter for my vertex arrays and pass it directly to glVertexPointer?

Not directly. That is two different data types. There are no garantees that the memory will be continuous, but I guess in most implementations it will be so anyway, but that is bad coding to make use of implementation specific details.

>> Are there any problems using STL with OpenGL?

As long as you don''t try to pass STL structures to OpenGL (as you suggest above) everything should be fine. Personally, I use STL in all my OpenGL apps.

Share this post


Link to post
Share on other sites
Its fine to pass the vector just as you would an array by using &vectorname[0] , since the C++ standard guarantees that vector implementations are constrained always to place data in contiguous memory (its basically a smart array). This is not the case for other data structures, so you should not go around passing the pointer to the first element of a std::list, for instance.

Share this post


Link to post
Share on other sites
>> since the C++ standard guarantees that vector implementations are constrained always to place data in contiguous memory

I can''t find this claim anywhere in the STL docs I have at hand. So I don''t think you are right. Can you provide a reference to somewhere authorative that says this?

Share this post


Link to post
Share on other sites
I got it from the Effective STL book, but after some research, it turns out you''re right; its not part of the standard yet (although its on the "defect" list and it likely will be added). everyone implements it this way; it would be bizarre not to, but you can always check your implementation.

see:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#69

Share this post


Link to post
Share on other sites
Also, from what I''ve been reading, it''s generally believed that the intent of the initial spec was that vectors should always use contiguous memory, and at this point, it is well beyond the discussion stage, and definitely *will* be clarified in the next spec.

Share this post


Link to post
Share on other sites
Well, yeah as my original post said I would expect most implementations to do so, but since the spec doesn''t state it there might be implementations out there doing it some different way. But if this (continuous vectors) is intended to be added soon to the spec because every implementation does so anyway then okay - I have learnt something new today.

This reminds me, is there any places on the web where the official standard for C++ is available? I have only read the standard indirectly via Stroustrups book and via SGI''s STL guide.

Share this post


Link to post
Share on other sites
hmmm, that sounds weird.

Since memory is dynamically allocated with vectors, what if the next area in memory (where the next vector element would go if you push_back) has something in it? When you create a vector, you dont specify what its maximum size will be so that enough continuous memory can be put aside for it. That would make vectors the same as normal arrays.

By assuming that every element in a vector is adjacent to eachother in memory, you are assuming that when the vector was created, enough continuous memory has been put aside for every element that will be in it, but with vectors, you dont specify a max size (whole point of using vectors, you dont know how much memory you will need)

Share this post


Link to post
Share on other sites
FearedPixel, this is because a vector may be reallocated when you extend it. In fact, the standard says that calling push_back invalidates all iterators to the vector. The reason for this is that the system might have to allocate a new continuous block of memory and copy the old data into this new area. So do not store pointers to array elements across push_back() calls or any other iterator invalidating methods!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!