-8

case 1:

std::vector< Ticker > snap_tickers_ (n_instruments);

and case 2:

std::vector< Ticker >snap_tickers_;
snap_tickers_.resize(n_instruments);

I am getting a compilation error when am trying case 2, whereas in case 1 am not getting any build failure. Can that be related to the type of object for which the vector is created?

ANSWER: resize in case 2 makes use of copy constructor, which was deleted for Ticker class, hence the failure.

Appy
  • 1
  • 1
  • The [reference material](http://en.cppreference.com/w/cpp/container/vector) would answer this – NathanOliver Aug 31 '16 at 16:15
  • Nathan, I looked into the material cited, couldn't find the answer.. – Appy Aug 31 '16 at 16:28
  • @Appy There's not much difference between these statements. The first defines the initial size in the constructor, whereas the second does it with a separate call of `resize()`. It's actually hard to believe that you couldn't figure this out from the linked reference documentation. – πάντα ῥεῖ Aug 31 '16 at 16:39
  • @πάνταῥε ῖI was trying to point out the same that i couldn't find any difference in the two scenarios. I thought them to be the same, the issue is i am getting a compilation error when am trying case 2, whereas in case 1 am not getting any build failure. Can that be related to the type of object for which the vector is created? – Appy Aug 31 '16 at 17:28
  • @Appy Best way to determine that is to replace `Ticker` with something trivial, like `int` and see if it still compiles (ignoring other later uses of your vector that expect `Ticker`). Also, it would help to include what your error is in the question – Altainia Aug 31 '16 at 17:45
  • @Appy To ask about compiler errors provide a [MCVE] that actually reproduces the problem and add the error messages verbatim in your post. As you see such kind of _shots in the dark_ don't work well here. – πάντα ῥεῖ Aug 31 '16 at 17:46
  • I find it really strange that you didn't find it relevant to post the *exact* text of the compiler error in your question, since that is likely to be *the* best clue to getting a relevant answer. – Jesper Juhl Aug 31 '16 at 19:12

2 Answers2

1

There is no real difference. case 1:

std::vector<int> vec(5);

allocates 5 int-elements.

case2:

std::vector<int> vec;
vec.resize(5);

here, we begin with an empty vector of ints. When you then call resize, the function checks if the size you passed over is smaller than the actual size (wich is 0, in that case). If yes, allocate _Newsize - size() new elements. If no, pop_back (delete) size() - _Newsize elements.

So in the end, resize is slower, because there are more machine cycles (if statements, subtracting sizes...) to do.

if you want to now more, here's the resize function from vector:

void resize(size_type _Newsize)
        {   // determine new length, padding as needed
        if (_Newsize < size())
            _Pop_back_n(size() - _Newsize);
        else if (size() < _Newsize)
            {   // pad as needed
            _Alty _Alval(this->_Getal());
            _Reserve(_Newsize - size());
            _TRY_BEGIN
            _Uninitialized_default_fill_n(this->_Mylast, _Newsize - size(),
                _Alval);
            _CATCH_ALL
            _Tidy();
            _RERAISE;
            _CATCH_END
            this->_Mylast += _Newsize - size();
            }
        }

as you can see, it does quite a lot. But in the end, it's just a question about (in most cases not important) micro-seconds... So no real difference.

Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • I am getting a compilation error when am trying case 2, whereas in case 1 am not getting any build failure. Can that be related to the type of object for which the vector is created? – Appy Aug 31 '16 at 17:41
  • mh. I have no clue why you'd get an error. That's relly wierd. Must come from the class Ticker. If that's your class, look if the = operator is overloaded. It should be! If not it may cause some troubles when using alogorithms. – Stack Danny Aug 31 '16 at 17:45
  • Yes i have overloaded '=' operator with delete operation as mentioned in this answer to prohibit copying: http://stackoverflow.com/a/5513928/5330679 – Appy Aug 31 '16 at 17:58
  • Ok I tried to make a class, and make a vector of it, then resize it. I had to remove the dekonstruktor and it worked. Maybe try that. – Stack Danny Aug 31 '16 at 18:03
  • it also worked when I removed the = operator, and the A(const A &other) – Stack Danny Aug 31 '16 at 18:06
  • How does this answer the question? – Peter VARGA Aug 31 '16 at 21:09
0

According to the C++ standard (since C++03), std::vector is required to store all elements contiguously,

[...] which means that elements can be accessed not only through iterators, but also using offsets on regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array.

Because of this restriction, resizing can potentially slow down performance because of the necessity of copying elements over to a new preallocated block. In practice, this overhead is usually only seen when resizing existing vectors with a lot of items requiring the vector to copy (or move) all of the objects to a new memory location.

In the example you gave, there is no real difference because the original vector had no items in it (and many compilers pre-allocate a chunk of memory to begin). I wouldn't be surprised if the compiler did an optimization to render equivalent code.

callyalater
  • 3,102
  • 8
  • 20
  • 27
  • I am getting a compilation error when am trying case 2, whereas in case 1 am not getting any build failure. Can that be related to the type of object for which the vector is created? – Appy Aug 31 '16 at 17:41
  • @Appy Possibly. `vector::resize` will default construct objects of type `T` when extending the vector. If there is no default constructor, you will most likely get a compilation error. – callyalater Aug 31 '16 at 19:39