when I pass vector v to sizeof which size does compiler shows

when I pass vector v to sizeof which size does compiler shows?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include<iostream>
#include<vector>
using namespace std;

int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(21);
    v.push_back(32);
    v.push_back(43);
    v.push_back(90);

    for(int i=0;i<5;i++)
    {
        cout<<v[i]<<endl;
    }

    cout<<endl;
    cout<<sizeof(v)<<endl;
    return 0;
}
Your program gives only one observation. So does this:
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<vector>

int main()
{
    std::vector<int> v( 31469, 42 );
    std::cout << sizeof(v) << '\n';
    std::cout << v.size() << '\n';
    std::cout << v.capacity() << '\n';
    return 0;
}

In principle it might be possible to draw a trend-line with two points.

Do remember that a vector<int> is essentially a
1
2
3
struct {
  int *;
};

(with some extras)
Variation on above code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<vector>

int main()
{
  {    
    std::vector<int> v( 31469, 42 );
    std::cout << sizeof(v) << '\n';
    std::cout << v.size() << '\n';
    std::cout << v.capacity() << '\n';
  }
  
  {    
    std::vector<int> v;
    for (int i=0; i<31469; ++i)
        v.push_back(42);
    std::cout << sizeof(v) << '\n';
    std::cout << v.size() << '\n';
    std::cout << v.capacity() << '\n';
  }
  
  
    return 0;
}

With the compiler I used, the capacity figure was different in the two examples.
Topic archived. No new replies allowed.