3

I was trying to create a pair type using dynamic memory and then return it's address to the calling function, but on checking the size of the pair object I got confused:

pair<int,vector<int> > vec;                          //not dynamic definition
vec=make_pair(3,vector<int> {1,2,3,4,5,8,9,0,6});    //9 element in vector
cout<<sizeof(vec.second)<<endl;

OUTPUT: 24

and on doing: sizeof(vec) the output is 32, size of int on my system is 4.

Can someone please explain how the size of 9 ints is 24 and after adding the first member of the pair it becomes 32?

Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
Agrudge Amicus
  • 1,033
  • 7
  • 19
  • It is not clear what is confusing you. Can you please clarify? What value do you expect? You are printing the size of only the second element of the `pair` in the first `sizeof`, while you print the full size of the `pair` in the second `sizeof`. – walnut Jan 12 '20 at 05:29
  • @walnut size of 9 `int`s is 36 but it's showing 24. – Agrudge Amicus Jan 12 '20 at 05:30
  • 3
    `sizeof` has nothing to do with the number of elements in a vector. It gives you the size of the `std::vector` object itself, not the size of the elements it manages. – walnut Jan 12 '20 at 05:31
  • @walnut that clarifies, how does the size of 1st member of pair becomes of 8 bytes? due to fixed object size like vector? – Agrudge Amicus Jan 12 '20 at 05:33
  • 3
    Does this answer your question? [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member) and [C++ sizeof Vector is 24?](https://stackoverflow.com/questions/34024805/c-sizeof-vector-is-24) – walnut Jan 12 '20 at 05:34
  • 3
    probably the vector has 8-byte alignment – M.M Jan 12 '20 at 05:35
  • 1
    check this out: https://stackoverflow.com/questions/34024805/c-sizeof-vector-is-24?noredirect=1&lq=1 – chai512 Jan 12 '20 at 05:48
  • 1
    @SebastianRedl: I don't think it was appropriate to close this question as a duplicate of the struct alignment issue. This question is about two issues: The mentioned struct alignment issue and the **vector size issue (which is not directly related to the alignment issue).** – Andreas Wenzel Jan 12 '20 at 06:39

1 Answers1

3

Despite the vector being called a "container", which it is from a logical perspective, from the perspective of the compiler the vector is just a (template) class which contains a pointer (or maybe several, depending on the implementation) to dynamically allocated memory and probably some other data members that are used for management of this memory. The vector class uses the dynamically allocated memory to expand and shrink, as needed.

Therefore, when using the sizeof operator on the actual vector, you only get the size of the data members that are used for vector management and not the size of the data the vector logically "contains".

Other "container" classes with fixed length, such as std::array or std::pair, are probably implemented without pointers to dynamically allocated memory, so using the sizeof operator on them will probably work as you intended. However, this is implementation-specific and cannot be relied upon.

The reason why the first element of the pair appears to take 8 bytes instead of 4 is probably an alignment issue. The first element contains a 4-byte member whereas the vector likely contains at least one 8-byte member (probably a 64-bit pointer).

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 1
    Since `std::vector` contains a pointer (or several), it must be aligned as such. So padding must be added after the `int` to align the `std::vector`. – Indiana Kernick Jan 12 '20 at 05:49
  • 1
    @Kerndog73: Thanks, for pointing that out. I have already edited my answer to address the alignment issue. – Andreas Wenzel Jan 12 '20 at 05:51