vector push_back zero into an empty vector

Ramon Martinez

I encountered the following property of std::vector that I did not know and I was wondering if someone could clarify it for me.

I think this code:

int main()
{
  int i = 5; 
  vector<int> vector;
  while(i > 0){
    vector.push_back(i);
    --i;
  }
}

should produce a vector with the following elements:

5, 4, 3, 2, 1

However, when I run the code above what I get is:

5, 4, 3, 2, 0

In order to explore what is going on I printed the vector at each stage and I get the following sequence:

  • i=5 -- vector (0)
  • i=4 -- vector(5, 0)
  • i=3 -- vector(5, 4, 0)
  • i=2 -- vector(5, 4, 3, 0)
  • i=1 -- vector(5, 4, 3, 2, 0)

So I think somehow a zero gets push_backed in the first cycle (where 5 should be) which is very strange. If I try a bigger (say n=10) then the last element is modified every time cycle.

I do not understand what's going. My guess is that there is something in the buffer maybe that get's push back before 5 and then this puts the push back and the cycle out of phase. Or is it something else?


Edit:


So as people kindly pointed out -or implied- in the comments the error was in the printing routine that I was used. For the record it was:

void print_vector(std::vector<int> vector){
  cout << string(10, '-') << endl;
  cout << "("; 
  for(int i=0; i < vector.size() - 1; ++i){
    cout << vector[i] << ", ";
  }
  cout << vector[vector.size()];
  cout << ")" << endl;

}

And the mistakes comes from the vector[vector.size()] which does weird things when the vector does not have elements I guess.

Erbureth says Reinstate Monica

Yes, your error is indeed in the printing code. The

vector[vector.size()]

expression is illegal in C++. Vector contains elements in range [0;vector.size()), and the expression tries to access the non-existent. one-past-end element of the vector, which is undefined behavior.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

vector push_back in STL?

From Dev

Vector and push_back() behavior

From Dev

Unable to push_back to vector

From Dev

push_back a vector of vectors into a vector

From Dev

time complexity of reserve on an empty vector vs. a deque and whether to use emplace or push_back

From Java

Insert or push_back to end of a std::vector?

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

vector::push_back and std::move

From Dev

Why are there two overloads for vector::push_back?

From Dev

string Vector push_back failing in class

From Dev

garbage values for vector push_back

From Dev

push_back new element to vector

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

garbage values for vector push_back

From Dev

Vector push_back incredibly slow

From Dev

Vector push_back Array of doubles

From Dev

Push_back variadic function parameters into a vector?

From Dev

c++ push_back() a struct into a vector

From Dev

vector of reference wrapper, push_back failure?

From Dev

push_back a pointer to a vector of pointers

From Dev

How to use vector iterators when using vector<>::push_back()

From Dev

Calling std::vector::push_back() is changing previous elements in vector?

From Dev

Calling std::vector::push_back() is changing previous elements in vector?

From Dev

How to use vector iterators when using vector<>::push_back()

From Dev

Filling empty list with zero vector using numpy

From Dev

C++ Vector: push_back Objects vs push_back Pointers performance

From Dev

vector push_back causes assertion error but list push_back works

From Dev

c++ vector - what's the difference between push_back(*new obj()) and push_back(obj())?

From Dev

Thread safety std::vector push_back and reserve

Related Related

  1. 1

    vector push_back in STL?

  2. 2

    Vector and push_back() behavior

  3. 3

    Unable to push_back to vector

  4. 4

    push_back a vector of vectors into a vector

  5. 5

    time complexity of reserve on an empty vector vs. a deque and whether to use emplace or push_back

  6. 6

    Insert or push_back to end of a std::vector?

  7. 7

    Implementatnion of std::vector::push_back in MSVC

  8. 8

    vector::push_back and std::move

  9. 9

    Why are there two overloads for vector::push_back?

  10. 10

    string Vector push_back failing in class

  11. 11

    garbage values for vector push_back

  12. 12

    push_back new element to vector

  13. 13

    Implementatnion of std::vector::push_back in MSVC

  14. 14

    garbage values for vector push_back

  15. 15

    Vector push_back incredibly slow

  16. 16

    Vector push_back Array of doubles

  17. 17

    Push_back variadic function parameters into a vector?

  18. 18

    c++ push_back() a struct into a vector

  19. 19

    vector of reference wrapper, push_back failure?

  20. 20

    push_back a pointer to a vector of pointers

  21. 21

    How to use vector iterators when using vector<>::push_back()

  22. 22

    Calling std::vector::push_back() is changing previous elements in vector?

  23. 23

    Calling std::vector::push_back() is changing previous elements in vector?

  24. 24

    How to use vector iterators when using vector<>::push_back()

  25. 25

    Filling empty list with zero vector using numpy

  26. 26

    C++ Vector: push_back Objects vs push_back Pointers performance

  27. 27

    vector push_back causes assertion error but list push_back works

  28. 28

    c++ vector - what's the difference between push_back(*new obj()) and push_back(obj())?

  29. 29

    Thread safety std::vector push_back and reserve

HotTag

Archive