32 位处理器真的可以寻址 2^32 个内存位置吗? [英] Can a 32-bit processor really address 2^32 memory locations?
问题描述
我觉得这可能是一个奇怪/愚蠢的问题,但这里...
I feel this might be a weird/stupid question, but here goes...
在问题 在 C 中是否需要/定义为 NULL是零吗?,已经确定NULL
指针指向一个不可寻址的内存位置,而且NULL
是0
.
In the question Is NULL in C required/defined to be zero?, it has been established that the NULL
pointer points to an unaddressable memory location, and also that NULL
is 0
.
现在,据说 32 位处理器可以寻址 2^32
个内存位置.
Now, supposedly a 32-bit processor can address 2^32
memory locations.
2^32
只是可以使用 32
位表示的不同数字的数量.在这些数字中有 0
.但是既然0
,也就是NULL
,应该是什么都不指向的,难道我们不应该说32位处理器只能寻址2^32吗?- 1
个内存位置(因为 0
不应该是有效地址)?
2^32
is only the number of distinct numbers that can be represented using 32
bits. Among those numbers is 0
. But since 0
, that is, NULL
, is supposed to point to nothing, shouldn't we say that a 32-bit processor can only address 2^32 - 1
memory locations (because the 0
is not supposed to be a valid address)?
推荐答案
NULL 指针指向一个不可寻址的内存位置
the NULL pointer points to an unaddressable memory location
这不是真的.从您链接的问题中接受的答案:
This is not true. From the accepted answer in the question you linked:
请注意,由于空指针规则的制定方式,您用于分配/比较空指针的值保证为零,但实际存储在指针中的位模式可以是任何其他内容
Notice that, because of how the rules for null pointers are formulated, the value you use to assign/compare null pointers is guaranteed to be zero, but the bit pattern actually stored inside the pointer can be any other thing
我所知道的大多数平台实际上都是通过将地址空间的前几页标记为无效来处理这个问题的.这并不意味着处理器不能解决这些问题;这只是使低值成为无效指针的便捷方法.例如,一些 Windows API 使用它来区分资源 ID 和指向实际数据的指针;低于某个值(如果我没记错的话是 65k)的所有内容都不是有效的指针,而是有效的资源 ID.
Most platforms of which I am aware do in fact handle this by marking the first few pages of address space as invalid. That doesn't mean the processor can't address such things; it's just a convenient way of making low values a non valid pointer. For instance, several Windows APIs use this to distinguish between a resource ID and a pointer to actual data; everything below a certain value (65k if I recall correctly) is not a valid pointer, but is a valid resource ID.
最后,仅仅因为 C 说了些什么并不意味着 CPU 需要以这种方式受到限制.当然,C 说访问空模式是未定义的——但是没有理由用汇编编写的人需要受到这样的限制.真正的机器通常可以做的比 C 标准所说的要多得多.虚拟内存、SIMD 指令和硬件 IO 是一些简单的例子.
Finally, just because C says something doesn't mean that the CPU needs to be restricted that way. Sure, C says accessing the null pattern is undefined -- but there's no reason someone writing in assembly need be subject to such limitations. Real machines typically can do much more than the C standard says they have to. Virtual memory, SIMD instructions, and hardware IO are some simple examples.
这篇关于32 位处理器真的可以寻址 2^32 个内存位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!