Memory Management – Value of Byte Pointed by Null Pointer

memorynullpointers

I know that long time ago computer scientists decided to treat all pointers to memory cell of address 0 as NULL. However, the memory cell at that address does exists after all, right? In that case, what value is stored there? Since it's OS that decides how to allocate memory it should be able to access (and modify) value of all cells in the RAM, including the one of index 0, right?

Yes, I have already seen this question. The thing is that I don't really ask where the NULL pointer actually points to. It is more of extension of former question as I want to know what the byte value of that memory cell is or what it depends on.

Best Answer

The physical 0 address corresponds indeed in general to a memory location in which some value is stored. The value is however system and environment dependent:

  • if the hardware maps this address to some ROM chip, the value is obviously fixed.
  • if it's RAM, at power-on the content is unpredictable, and you should consider it random. It's however not really random : it could contain some previously stored value. This property is used in cold boot attacks.
  • The BIOS/UEFI/OS could would clear it to 0 at startup. Some process could store their variable there.

But nowadays, most CPU and OSes use virtual memory. In such a scheme, every process only sees a virtual address space. This space is organized in "pages" (blocks of contiguous memory) that are mapped to physical address, not mapped, and/or swapped to some disk space by a page handler.

So the address N of one process will not be the address N of another process. And in particular, the virtual address 0 might not be teh physical address 0. It could well remain unmapped, causing a hardware exception (page fault) for every access attempt (more explanations here).