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

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

In the question, Is NULL in C mandatory / defined to be zero? , it was found that a NULL pointer indicates a non-addressable memory location and also that NULL 0 .

Now, presumably, a 32-bit processor can address 2^32 memory locations.

2^32 is just the number of different numbers that can be represented using bit 32 . Among these numbers, 0 . But since 0 , i.e. NULL , should indicate nothing, should we say that a 32-bit processor can only address memory addresses 2^32 - 1 (since 0 not supposed to be a valid address)?

+7
source share
4 answers

NULL pointer points to a non-addressing folder

This is not true. From the accepted answer in your related question:

Please note that due to the way the rules for null pointers are formulated, the value that 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.

Most of the platforms that I know about actually handle this by putting the first few addresses of the address space as invalid. This does not mean that the processor cannot consider such things; it's just a convenient way to make low values ​​an invalid pointer. For example, several Windows APIs use this to distinguish between a resource identifier and a pointer to actual data; everything under a specific value (65k, if I remember correctly) is not a valid pointer, but a valid resource identifier.

Finally, just because C says that something does not mean that the processor should be limited in this way. Of course, C says access to the null template is undefined - but there is no reason for someone who wrote in the assembly to be subject to such restrictions. Real machines can usually do much more than they say in the C standard. Virtual memory, SIMD instructions, and hardware IOs are simple examples.

+8
source

If a 32-bit processor can address 2 ^ 32 memory locations, it simply means that the C pointer on this architecture can refer to 2 ^ 32 - 1 locations plus NULL.

+9
source

It depends on the operating system. This is related to virtual memory and address spaces.

In practice (at least on Linux x86 32 bits) addresses are byte "numbers", but most of them refer to 4-byte words, so they are often a multiple of 4.

And more importantly, as seen from the Linux application, only the largest 3Gbtes of 4Gbytes are visible. a whole gigabyte of address space (including the first and last pages, next to the null pointer) is not displayed. In practice, the process is much smaller. See Its pseudo-file /proc/self/maps (for example, run cat /proc/self/maps to see the address card for the cat on Linux).

0
source

First, pay attention to the difference between a linear address (AKA - pointer value) and a physical address. While the linear address space is indeed 32 bits (AKA 2 ^ 32 different bytes), the physical address that goes to the memory chip does not match. Parts (β€œpages”) of the linear address space can be mapped to physical memory or to a page file or to an arbitrary file, or marked as inaccessible and not backed up by anything. The last page is the last. The display engine is implemented at the CPU level and is supported by the OS.

However, the null address, which is a non-addressing memory, is simply a C convention, which has been provided by all protected operating systems since the first Unices. On real-time operating systems in MS-DOS mode, the null pointer (0000: 0000) was completely addressable; however, writing there will destroy system data structures and bring only trouble. A null pointer (DS: 0000) was also completely accessible, but the runtime library usually reserved some space around zero to protect against accidental dereferencing of null pointers. In addition, in real mode (for example, in DOS), the address space was not 32-bit flat, it was 20-bit efficient.

0
source

All Articles