Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good application of std::vector::data? [closed]

So my friend and I were looking at the std::vector class the other day and noticed that you can use the function std::vector::data to retrieve a pointer to the internal array in c++11. Intuitively this seems somewhat redundant as you can access/update the contents of the internal array using the [] on the vector anyway.

It just seems like bad practice to me to "stick your hand into" an STL container.

Anyway, there must be some good applications of it. So my question is, what are those applications?

Thanks!

Reference: http://www.cplusplus.com/reference/vector/vector/data/

like image 942
Kittenmittons Avatar asked Oct 17 '25 15:10

Kittenmittons


1 Answers

For example if you need to pass the data to a function that only takes a pointer. OpenGL for example has a bunch of them (glBufferData for example). std::vector makes the memory management easy, so it's a good choice instead for a container instead of managing the memory oneself and potentially leaking it somewhere. It's not meant that you mutate the vectors content through the returned pointer, but rathe read it and/or pass it to functions expecting a pointer.

like image 90
JustSid Avatar answered Oct 19 '25 06:10

JustSid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!