0

I read the following code piece.

vector<vector<int>> result;
//level is an integer. 
if (level > result.size())
     result.push_back(vector<int>());

I'm wondering: What does vector<int>() create, an object or a pointer of vector?

I think it should be a vector object, instead of a pointer of vector. Otherwise, it won't compile.

However, I feel that vector<int>() is similar to new vector<int>() .

Maybe I missed something?

I really appreciate it if you could point out the knowledge point I missed.

Thank you very much!

Mike
  • 1,841
  • 2
  • 18
  • 34

1 Answers1

3

Both cases are similar in that they provide a real, honest-to-god object. The differences are in how you access it and how long it lives.

vector<int>() creates an object in Automatic storage with Automatic storage duration. You can consider this one an object that you can hold in your digital hand, but only temporarily. Without an associated identifier it goes out of scope and vanishes, is destroyed, at the end of the line. Before destruction it is copied into result. With an identifier (eg. vector<int> vec;), an Automatic variable lasts until the identifier reaches the end of its scope.

new vector<int>() creates an object in Dynamic storage with Dynamic storage duration and gives you a pointer to it. This object is out there somewhere and all you "hold" is information about how to find it. Couple problems in this case. You've noticed the first, result will not accept a pointer to a vector. It wants the real thing. The other problem is objects with Dynamic storage duration hang around until they are manually destroyed with delete or the process ends. Unless you have a pointer to it, the memory is lost, floating through space without a tether you can use to access and delete it.

In general, avoid new unless you find one of the rare cases where you cannot avoid it, and in those cases prefer a smart pointer.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    nit "makes an object in Dynamic storage" -> "creates an object with *allocated storage duration* with life that extends until it is deleted or the program ends" (but kudos for the accurate and colorful description) – David C. Rankin May 03 '18 at 20:38
  • Can I think in this way: vector() return a reference. Without assigning the reference to a variable (or without using the reference), the object of the reference is destroyed automatically? This is like the reference mechanism in Java? – Mike May 03 '18 at 21:29
  • @Mike Best not to. [A reference in C++ is its own thing](https://stackoverflow.com/questions/2765999/what-is-a-reference-variable-in-c), so be careful how you use the term. While the destruction of an object in Java and a Automatic variable in C++ is handled automatically for you that's where the similarity ends. – user4581301 May 03 '18 at 23:29
  • The biggest difference that you can notice (the stuff going on behind the scenes can be dramatically different) is probably that you never know exactly when a Java object will be destroyed, but the Automatic C++ variable is destroyed exactly on schedule and in a predefined order. [This guarantee allows you to take precise control of resources.](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) Closely following this is the notion of [Value Semantics](https://isocpp.org/wiki/faq/value-vs-ref-semantics) – user4581301 May 03 '18 at 23:31
  • Interesting side-reading on one of the big ideological differences: [Raymond Chen's Everybody thinks about garbage collection the wrong way](https://blogs.msdn.microsoft.com/oldnewthing/20100809-00/?p=13203) – user4581301 May 03 '18 at 23:44