0

I'm a complete beginner to C++ but I have some basics knowledge in programming (Python mainly) and I'm trying to learn C++. As the question implies, vectors have static sizes (at least what I've read in my learning material) but we still can add more values to what the size authorize. I wrote a simple code to know what error I get if I pass more values to a vector than the limit authorized by it's size and surprisingly I didn't get any error.

The code are these simple lines:

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int it=0,a;
    vector<int> v(10); 
    for(a=1; a<21; a++)
    {
            v[it]=x;
            cout << v[it] << endl;
            it++;
    }
    cout<<"Values stored in v";
    for(i=0;i<it;i++)
        cout<<v[i]<<" ";
    cout<<endl;
    cout<<"Vector's size : "<<v.size()<<endl;
    return 0;
}

What I get with cout<<"Values stored in v"; are all values from 1 to 20, but I still get that the size is 10.

If that can helps I'm on Windows 10 x64 and using Qt Creator compiler.

Daviiid
  • 119
  • 5
  • 1
    "vectors have static sizes (at least what I've read in my learning material)" this is a bit unclear, where did you read that? – 463035818_is_not_an_ai Feb 01 '21 at 10:45
  • The definition `vector v(10);` defines the variable `v` as a `std::vector`, containing `10` (and only `10`) elements. Any decent book ([here's a list of good ones](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)), tutorial or class should have taught you how to append elements to a vector. – Some programmer dude Feb 01 '21 at 10:48
  • If I understand you correctly, `vector` does *not* have static size, it can grow as needed. Arrays (both built-in and `std::array` type) have fixed size. – jkb Feb 01 '21 at 10:50
  • **largest_prime_is_463035818** on a recommended website so I guess it's sure and with your answer below I just understood that the fixed size is more in the sense of you can't access elements afterwards. At the beginning I thought about it as a Python dictionnary but it's more like a list (you need a certain method to extend the size). **Some programme**r thank you for the link ! **jkb** Yes thank you, at first I didn't understand that ^^' – Daviiid Feb 01 '21 at 11:11
  • to ping a user you need to prepend their name with `@`. – 463035818_is_not_an_ai Feb 01 '21 at 11:23

1 Answers1

2

What is fixed is

sizeof( vector<int> )

A vector can contain varying number of elements, but that elements are stored on the heap, hence do not contribute to the vectors sizeof. The number of elements is v.size() (and that can change).

You create a vector with 10 elements:

vector<int> v(10); 

But then you attempt to access elements that do not exist in the loop. For example v[10] will not cause a compiler error, it is also not guaranteed to cause a runtime error. Nevertheless, it is guaranteed wrong. Accessing the vector out of bounds causes undefined behavior. The output of the code could be anything.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you for your answer. So v.size() can change from 0 (the vector is empty) to 10 right ? I find it weird that the output could be anything but still, each time I run the code, the output is the same. Maybe there are some fixed rules to any compiler on how to deal with these undefined behaviors. – Daviiid Feb 01 '21 at 11:02
  • @Daviiid not sure if I understand you. You asked for a vector of 10 elements, because you pass 10 to the constructor. To change the size afterwards you can call `resize` or `push_back` to add an element – 463035818_is_not_an_ai Feb 01 '21 at 11:04
  • Yes, I just wanted to know what error is raised and I was surprised by the output so I thought that there were elements added to the vector but the problem lays in the loop itself (if I understood your answer), it's trying to access elements that don't exist. And so it's just a matter of compiler I guess on how to handle these things. – Daviiid Feb 01 '21 at 11:07
  • @Daviiid "Maybe there are some fixed rules to any compiler on how to deal with these undefined behaviors." no, it is just the opposite: The C++ standard defines what code does that has defined behavior. It does not specify what code does that has undefined behavior. Sloppy speaking, once you break the rules, rules don't matter anymore – 463035818_is_not_an_ai Feb 01 '21 at 11:10
  • @Daviiid - The behaviour is undefined according to the standard - which means simply that the standard does not define (or constrain) what can happen. Faced with code that exhibits undefined behaviour, compilers are permitted to do *anything* (from pretending there is nothing wrong, not generating any code at all, reformatting your hard drive, melting your CPU) and - whatever a compiler does (no matter how benign or harmful it is) it is still correct. – Peter Feb 01 '21 at 11:15
  • @largest_prime_is_463035818 oh okay thank you ! I thought it was weird I'm getting each time the same output, maybe there's something behind that's different. One last question can you tell me please why do we specify the size of a vector if we can just change it later with push_back and since you can get undefined behaviors by error ? I mean why not just make vecotrs like Python's lists ? – Daviiid Feb 01 '21 at 11:17
  • @Daviiid C++ Is not python. If `[]` would automatically resize the vector this would cost performance, even worse: You only would benefit in particular cases (access an index out of bounds) but you would have to pay always. Thats not acceptable for a generic container. If you want a container that behaves like that it is possible to write some helper code that makes a vector behave like that. Eg you could make it such that you can call `access_and_resize_if_necessary(v,12) = 123;` – 463035818_is_not_an_ai Feb 01 '21 at 11:20
  • @largest_prime_is_463035818 I see, thank you a lot it was very interesting and enriching. – Daviiid Feb 01 '21 at 13:00
  • @Peter thank you for your answer, I understand now. – Daviiid Feb 01 '21 at 13:02