Many people recommend vector class for variable length array. And I have to pass pointer to memory block to GL. How can I access pointer to memory block allocated by std::vector
?
Vectors are assigned memory in blocks of contiguous locations. When the memory allocated for the vector falls short of storing new elements, a new memory block is allocated to vector and all elements are copied from the old location to the new location.
As mentioned above, std::vector is a templated class that represents dynamic arrays. std::vector typically allocates memory on the heap (unless you override this behavior with your own allocator). The std::vector class abstracts memory management, as it grows and shrinks automatically if elements are added or removed.
The elements of a vector are stored in a dynamically allocated block of memory; otherwise, the capacity of the vector could not increase. The vector object just holds a pointer to that block.
Yes, the elements of a std::vector are guaranteed to be contiguous.
Use the address of first element. If your vector is v
then &v[0]
will work.
ContainerType* pData = &vec.front();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With