std::vector is guaranteed to have contiguous memory. So vector.front() or &(vector[0]) are legal references to the first element of a c-style array. That is why it is important to preallocate vectors to the expected length when possible, to avoid the reallocation hit necessary to preserve contiguous memory. std::deque has similar accessors, but is not contiguous, so reallocation is much cheaper, but you cant use as a c-array.
It's not a hack, it's documented and legal behavior. -- mew On Wed, February 27, 2008 16:06, Night Hawk wrote: > Hello all, > > I was going through the source-code of Array class in the OSG and was > wondering the data-conversion efficiency between STL::Vector (used by OSG > Arrays) and the underlying OpenGL C-Style Arrays. > > However, I came across this usage where vector.front() is used to treat > the > vector members as a sequential c-style array. (For example, > glVertexPointer() is passed a vector.front())) > > My question is, is this a vector class Hack or is it a standard feature? I > have never seen it documented anywhere that that vector::front() returns a > pointer to the underlying c-style array (that we can use to sequentially > access all array elements). > > Am I missing something here? > > Also, the TemplateArray constructor had a simple std::vector<T>(ptr, > ptr+count) type of argument that "magically" converted the c-style array > to > a vector !! > > I have gone through the documentation of the vector() constructors and it > said it accepts an iterator. But I never imagined we can use the array > pointer itself as iterator !! > > Surprising indeed. I was about to override the Array classes to use native > c-style arrays. But now it seems OSG can convert vectors to C-style arrays > fast enough :) > > Wish the documentation (either for vector or for OSG) can cover this. > People > like me who have seen "NO STL for gaming" slogans are taken aback when we > see OSG using STL heavily. > > Thanks all. > _______________________________________________ > osg-users mailing list > [email protected] > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > Mike Weiblen -- Austin Texas USA -- http://mew.cx/ _______________________________________________ osg-users mailing list [email protected] http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

