数组的物理内存地址是否也像虚拟地址一样按顺序存储? [英] Are the physical memory addresses of an array also stored in order like the virtual ones?

查看:28
本文介绍了数组的物理内存地址是否也像虚拟地址一样按顺序存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是当我们在 C++ 中有一个表时:int tab[3] = {1,2,3}; 我们知道如果 tab 地址是 X 那么 tab[1] 的地址是 X + 1.我一直在阅读一些关于虚拟和物理地址的信息,所以我猜这些地址是虚拟的.所以如果在物理内存中 tab 是 Y 地址,那么 tab[1] 是 Y+1 还是不必?

I mean when we have in C++ a table: int tab[3] = {1,2,3}; we know that if tab address is X then address of tab[1] is X + 1. I've been reading a bit about virtual and physical addresses, so I guess these addresses are virtual. So if in physical memory tab is Y address then tab[1] is Y+1 or doesn't have to?

推荐答案

First;页面内的所有连续内容对于虚拟内存和物理内存都是连续的.

First; everything that's contiguous within a page will be contiguous for both virtual memory and physical memory.

如果数组碰巧跨越了页面边界(例如,前半部分在一个页面中,后半部分在另一页面中),或者如果数组太大而无法放入 2(或 3)页,那么它仍然会在虚拟内存中是连续的,但在物理内存中可能不连续.

If the array happens to be split across a page boundary (e.g. first half in one page, second half in another page), or if the array is too big to fit in 2 (or 3) pages, then it will still be contiguous in virtual memory, but may not be contiguous in physical memory.

对于大多数情况,物理内存中不连续"并不重要.

For most things, "non-contiguous in physical memory" doesn't matter.

但是;对于(直接映射和关联)缓存,存在潜在的效率损失.要理解这个想象,如果有一个 64 KiB 的缓存并且您有一个 64 KiB 的数组,但是物理地址碰巧使用缓存中的相同条目 - 您最终可能会导致数组争夺相同的少量缓存(并且被无法使用大部分缓存).

However; for (direct mapped and associative) caches there's potential efficiency loss. To understand this imagine if there's a 64 KiB cache and you have a 64 KiB array, but the physical addresses happen to use the same entries in the cache - you can end up with the array fighting for the same small amount of cache (and being unable to use most of the cache).

为了防止这个问题,好的操作系统会使用一种叫做页面着色"或缓存着色"的东西;这主要意味着分配物理页面,以便页面(对于确定数据最终在缓存中的哪个位置很重要的地址位)看起来是连续的(即使物理页面实际上并不连续).您可以在此处阅读更多相关信息:https://en.wikipedia.org/wiki/Cache_coloring

To prevent this problem good operating systems use something called "page colouring" or "cache colouring"; which mostly means that physical pages are allocated so that the pages (the bits of the address that matter for determining where in the cache the data would end up) seem contiguous (even if the physical pages are not actually contiguous). You can read more about this here: https://en.wikipedia.org/wiki/Cache_coloring

这篇关于数组的物理内存地址是否也像虚拟地址一样按顺序存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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