std::vector 元素在物理内存中是连续的吗? [英] Are std::vector elements contiguous in physical memory?

查看:64
本文介绍了std::vector 元素在物理内存中是连续的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于this,但是我问一些不同的东西.

My question is similar to this, however I'm asking something a bit different.

很明显,可以将第一个 std::vector 元素的地址用作 C 类型数组.这意味着在虚拟内存中,std::vector 元素是连续的.但是,如果物理内存是碎片化的,那么std::vector实际上可能在物理内存中被分割成很多部分.

It is clear, that it is possible to use the address of the first std::vector element as a C type array. That means that in virtual memory, std::vector elements are contiguous. 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)?

推荐答案

用于在向量中存储数据的内存必须位于连续地址,因为这些地址对代码可见.

The memory used to store the data in a vector must be at contiguous addresses as those addresses are visible to the code.

在大多数现代 CPU/OS 的典型情况下,这意味着虚拟地址必须是连续的.如果这些虚拟地址跨越页面边界,那么物理地址很可能不再连续.

In a typical case on most modern CPUs/OSes, that will mean the virtual addresses must be contiguous. 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.

这篇关于std::vector 元素在物理内存中是连续的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆