简体   繁体   English

一组向量是完全连续的内存吗?

[英]Is an array of vectors entirely contiguous memory?

I know vectors are guaranteed to be contiguous memory, and so are arrays. 我知道向量保证是连续的内存,数组也是如此。 So what happens when I do something like this: 那么当我做这样的事情时会发生什么:

std::vector<uint8_t> my_array[10];
my_array[2].push_back(11);
my_array[2].push_back(7);

What would the memory look like? 记忆会是什么样的? If both need to be contiguous, would every element of the array after my_array[2] be pushed forward a byte every time I do a push_back() on my_array[2] ? 如果两者都需要连续,那么每次在my_array[2]上执行push_back()my_array[2]之后的数组的每个元素都会被推送一个字节吗?

Would this be the same situation as when I have an array of structs, where the structs have a member that has a variable size, such as a string or another vector? 这是否与我有一个结构数组时的情况相同,其中结构体有一个具有可变大小的成员,例如字符串或另一个向量?

Memory footprint of std::vector consists of two parts: std::vector内存占用由两部分组成:

  • The memory for the std::vector object itself (very small, and independent of the size), and std::vector对象本身的内存(非常小,与大小无关),以及
  • The memory for the data of the vector (depends on the number of elements in the vector). 向量数据的存储器(取决于向量中的元素数)。

The first kind of data will be contiguous in an array; 第一种数据在数组中是连续的; the second kind of data is allocated dynamically, so it would not be contiguous in an array. 第二种数据是动态分配的,因此它在数组中不是连续的。

This would not be the same as with a C struct that has a flexible data member, because the data portion of std::vector is not always allocated in the same kind of memory, let alone being adjacent to it. 这与具有灵活数据成员的C struct ,因为std::vector的数据部分并不总是分配在同一种内存中,更不用说与它相邻。 The vector itself may be allocated in static, dynamic, or automatic memory areas, while its data is always in the dynamic area. 矢量本身可以分配在静态,动态或自动存储区域中,而其数据总是在动态区域中。 Moreover, when vector is resized, the memory for its data may be moved to a different region. 此外,当调整矢量大小时,其数据的存储器可以移动到不同的区域。

Each time you call push_back , std::vector checks if it has enough dynamic memory to accommodate the next data element. 每次调用push_backstd::vector检查它是否有足够的动态内存来容纳下一个数据元素。 If there is not enough memory, then the vector allocates a bigger chunk of memory, and moves its current content there before pushing the new item. 如果内存不足,则向量会分配更大的内存块,并在推送新项目之前将其当前内容移动到那里。

The vector memory structure is contiguous in memory; 向量存储器结构在存储器中是连续的; however std::vector's all contain a pointer pointing to dynamically allocated memory for the actual storage (which is very very likely not contiguous). 但是std :: vector都包含一个指针,指向实际存储的动态分配内存(很可能不是连续的)。

Knowing this, std::vector::push_back will only check to see if the (external) dynamically allocated array has enough capacity to hold the new item, if not it will reallocate space. 知道了这一点,std :: vector :: push_back只会检查(外部)动态分配的数组是否有足够的容量来容纳新项,否则会重新分配空间。 A push_back on the first vector that overflows will not cause the second vector in the array to reallocate memory, that isn't how it works. 第一个向量溢出的push_back不会导致数组中的第二个向量重新分配内存,这不是它的工作方式。

Also, there is no such thing as a struct having a variable size, the size of structures and classes have to be known at compile time. 此外,没有结构具有可变大小,结构和类的大小必须在编译时知道。

std::string also has a fixed size, although you may think it is variable, because it also (like vector) has a pointer to the char* it contains. std :: string也有一个固定的大小,虽然你可能认为它是可变的,因为它也(像vector)有一个指向它包含的char*的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM