The difference in the memory layout of a vector of pairs and a vector of structures containing two elements - C ++ / STL

Is the layout in memory test1 and test2 the same?

 std::vector<std::pair<int,int> > test1; std::vector<mystruct> test2; 

where mystruct is defined as:

 struct mystruct{ int a; int b; }; 
+4
source share
4 answers

Logically, std::pair<int, int> should also be defined.

However, there is nothing on the standard, and it is not fully guaranteed. You can look at the header files in your compiler to confirm this, but this proves nothing.

Note. . If you consider it absurd to be different, I can give you an example of how this can be defined otherwise. Imagine that in other classes of stl templates that use std::pair , they think that it would be convenient (for some reason) to have a pointer to a node containing a pair in a pair. That way, they could internally add a pointer to the class of the pair without breaking any rules. Then your assumption will lead to chaos. I would say that they are unlikely to do such things, yes, but as long as they are not forced into this scheme, anything can happen.

+3
source

If you see on cplusplus.com, you will see that this is a pair structure:

 template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair() : first(T1()), second(T2()) {} pair(const T1& x, const T2& y) : first(x), second(y) {} template <class U, class V> pair (const pair<U,V> &p) : first(p.first), second(p.second) { } } 

In the same way, I would say, except for some facts: Well, starting with the fact that pairs are compatible with std containers and all this, for example, with maps. In addition, pairs have already been created and already have constructors for you.

EDIT: I also forgot to mention that you will have std :: make_pair for you, which allows you to skip the memory allocation and create your own pair in the structure, and you also have certain comparison and assignment operators.

+2
source

Yes, at least in Release mode, it will be the same size.

You should not use this knowledge to do some memory tricks, although for two reasons:

1) Std uses many additional debugging materials. This causes class sizes to vary in debug and release mode. Therefore, it is entirely possible that std :: pair is larger in debug mode.

2) STD may change internally. There is no guarantee that std :: pair will not change the memory layout in another STD version. Therefore, if you rely on the fact that you need to live with fear, that one day he will no longer be able to work.

+1
source

In any reasonable std::pair implementation, they will have the same layout. But that begs the question of why it matters? You must not perform binary assignments from one to another.

You can add a copy constructor to the structure, either directly or by subclassing. You can also add a method that converts another way.

 struct mystruct{ int a; int b; mystruct(const std::pair<int,int> &rhs) { a=rhs.first; b=rhs.second; } std::pair<int,int> as_pair() { return std::make_pair(a, b); } }; 
+1
source

Source: https://habr.com/ru/post/1386594/


All Articles