Can a 32-bit processor really address 2^32 memory locations?

前端 未结 4 1801
抹茶落季
抹茶落季 2021-01-12 03:29

I feel this might be a weird/stupid question, but here goes...

In the question Is NULL in C required/defined to be zero?, it has been established that the NULL

4条回答
  •  余生分开走
    2021-01-12 04:01

    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

    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.

    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.

提交回复
热议问题