简体   繁体   English

std :: vector元素在物理内存中是连续的吗?

[英]Are std::vector elements contiguous in physical memory?

My question is similar to this , however I'm asking something a bit different. 我的问题与类似,但我问的是有点不同。

It is clear, that it is possible to use the address of the first std::vector element as a C type array. 很明显,可以将第一个std::vector元素的地址用作C类型数组。 That means that in virtual memory, std::vector elements are contiguous. 这意味着在虚拟内存中, std::vector元素是连续的。 However, if physical memory is fragmented, it is possible that std::vector is actually split into many parts in the physical memory. 但是,如果物理内存是碎片化的,则std::vector实际上可能会分成物理内存中的许多部分。

My question is: Are std::vector elements contiguous in physical memory (as well as virtual memory)? 我的问题是: std::vector元素在物理内存(以及虚拟内存)中是连续的吗?

The memory used to store the data in a vector must be at contiguous addresses as those addresses are visible to the code. 用于在向量中存储数据的存储器必须位于连续的地址,因为这些地址对于代码是可见的。

In a typical case on most modern CPUs/OSes, that will mean the virtual addresses must be contiguous. 在大多数现代CPU /操作系统的典型情况下,这意味着虚拟地址必须是连续的。 If those virtual addresses cross a page boundary, then there's a good chance that the physical addresses will no longer be contiguous. 如果这些虚拟地址跨越页面边界,那么物理地址很可能不再是连续的。

I should add that this is only rarely a major concern. 我应该补充一点,这很少是一个主要问题。 Modern systems have at least some support for such fragmented memory usage right down to the hardware level in many cases. 在许多情况下,现代系统至少对这种分散的内存使用有一定的支持,直到硬件级别。 For example, many network and disk controllers include "scatter/gather" capability, where the OS uses the page tables to translate the virtual addresses for the buffer to physical addresses, then supplies a number of physical addresses directly to the controller, which then gathers the data from those addresses if it's transferring from memory to peripheral or "scatters" the data out to those addresses if it's transferring from peripheral to memory. 例如,许多网络和磁盘控制器都包含“分散/聚集”功能,其中操作系统使用页表将缓冲区的虚拟地址转换为物理地址,然后直接向控制器提供多个物理地址,然后控制器收集来自这些地址的数据,如果它从内存转移到外设或者将数据“分散”到这些地址,如果它从外设转移到内存。

No, there is no guarantee that you will be provided contiguous physical memory in C++'s abstract machine. 不,不能保证你会在C ++的抽象机器中提供连续的物理内存。 Abstractions and hardware below malloc are free to use discontiguous memory. malloc下面的抽象和硬件可以自由使用不连续的内存。

Only your targeted implementation could make such a guarantee, but the language/model does not care. 只有您的目标实施可以做出这样的保证,但语言/模型并不关心。 It relies on the system to do its job. 它依靠系统来完成它的工作。

Virtual to physical memory mapping is handled largely by the CPU, but with kernel support. 虚拟到物理内存映射主要由CPU处理,但内核支持。 A userland process cannot know what this mapping is: your program, no matter what the programming language, deals solely in virtual memory addresses. 用户空间进程无法知道此映射是什么:无论编程语言是什么,您的程序仅处理虚拟内存地址。 You cannot expect, nor is there any way of even finding out, if two adjacent virtual memory addresses that straddle a page boundary are adjacent in physical memory, so there is absolutely no point worrying about it. 如果跨越页面边界的两个相邻虚拟内存地址在物理内存中相邻,那么您无法预料甚至找不到任何方法,因此绝对没有必要担心它。

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

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