[Edit] I didn't read the other replies careful enough and managed to repeat a lot of information except how to change the deleter!
[Edit] Actually I removed my answer completely because it is wrong to change the deleter. std::unique_ptr<int[]> selects the corret deleter already. Listen to Edd!
std::vector vs std::unique_ptr
Is it allowed to use &v[0], where v is a std::vector?
Yes, that is legal.
It seems to me then that the advantage of std::unique_ptr (compared to std::vector) is when you want to ensure it is not copied?
That would certainly be an important consideration, and possibly a determining factor.
I tested with a function that defined a local std::vector, initialized it, and then returned it. Initializing a variable with the result of this function did indeed reference the same memory area, which means there was an implicit move. I wonder how that works (though it did what I wanted).
First, initializing a variable with the result of a function calls the move constructor, which std::vector has.
Second, if the object doesn't have move constructor, RVO (return value optimization) is probably performed by the compiler.
One use-case I have, is to create a vector of data that need to be temporarily forwarded externally (in this case a vertex attribute list forwarded to an OpenGL VBO). Using std::unique_ptr, I can use the get() function to get a pointer. Is it allowed to use &v[0], where v is a std::vector?
It is not only allowed, it is considered idiomatic.
It seems to me then that the advantage of std::unique_ptr (compared to std::vector) is when you want to ensure it is not copied?[/quote]
A big advantage of using a vector is that you're dealing with a first-class object. It tells the reader "I have a buffer and I'm using it to hold data". Using any kind of pointer instead and you tell the read "I have an address to some memory somewhere, and I'm using that address to do stuff indirectly to that memory". Turns out in the world of professional software development, much more time is spent in maintenance than in development. Being able to communicate intent as clearly as possible is the mark of the best code. the more you can work in the problem domain rather than the implementation domain, the clearer your intent tends to be.
Turns out in the world of professional software development, much more time is spent in maintenance than in development. Being able to communicate intent as clearly as possible is the mark of the best code. the more you can work in the problem domain rather than the implementation domain, the clearer your intent tends to be.
I certainly appreciate that, having used Google Go rather than C++ a couple of years now. So far, my productivity has been about 3 times as high with Go as with C++. Proper use of smart pointers and containers in C++ is an important step towards that goal.
Please explain in what way it is misleading? std::vector is used to manage vectors. std::unique_ptr is used to manage pointers to objects or vectors. So it is rather obvious what I was asking for, isn't it?
The title of this thread is "std::vector vs std::unique_ptr". I'm pretty sure most people started to read it, because they were wondering how one could possibly meaningfully compare those two and not because they want to discuss vector vs. array, which has been discussed to death already.
I don't understand this. A vector is a one dimensional array. So for the one dimensional case, there are no differences by definition.
In your original post you asked "What are the general guidelines here?" for "std::vector vs std::unique_ptr" (to a T[]).
I gave you a general guideline, that is very simple and very effective.
If you look at vector vs. array from the perspective, that both of them store a sequence of values in a continuous chunk of memory, then yes, there is no difference. However, there are differences between "std::vector vs std::unique_ptr" (to a T[]), which is, if I understood you at all, what you asked for. And the answer is that vector is preferable in pretty much every case, because it offers more functionality and safer interface, at the cost of approximately 2 extra pointers.
If you're so starved on memory, that you can't afford those 2 extra pointers, you'll need to stay away from allocations on the free-store anyways, so unique_ptr to a T[] is no help either.
I hope that clarifies things!
[quote name='larspensjo' timestamp='1340919565' post='4953767']One use-case I have, is to create a vector of data that need to be temporarily forwarded externally (in this case a vertex attribute list forwarded to an OpenGL VBO). Using std::unique_ptr, I can use the get() function to get a pointer. Is it allowed to use &v[0], where v is a std::vector?
It is not only allowed, it is considered idiomatic.
[/quote]
In C++11 this can now be replaced with the data() method, which returns a pointer to the first value just like &v[0], but with a bit clearer intent. It's also a const pointer, which &v[0] doesn't provide.
Since in C++11 std::unique_ptr replaced Boost's boost::scoped_ptr /* http://stackoverflow...aking-ownership */, you might as well compare with boost::scoped_array: http://www.boost.org...coped_array.htm
For some discussion(s), see:
http://www.progtown....std-vector.html
http://www.gamedev.n...s-scoped_array/
http://www.tebyan.ne...x=22&Language=3
http://lists.boost.o...1/10/187258.php
For some discussion(s), see:
http://www.progtown....std-vector.html
http://www.gamedev.n...s-scoped_array/
http://www.tebyan.ne...x=22&Language=3
http://lists.boost.o...1/10/187258.php
More importantly, it works when the vector is empty too!
In C++11 this can now be replaced with the data() method, which returns a pointer to the first value just like &v[0], but with a bit clearer intent. It's also a const pointer, which &v[0] doesn't provide.
So nobody mentioned std::array yet huh?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement