1

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?

edit: 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.

3

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).

  • Following the NULL pointer is a common error. Some operating systems do not map the page at address 0 to force a memory exception when a NULL pointer is used. – BillThor Dec 11 '16 at 3:04
  • @BillThor dereferencing a NULL pointer in C or a nullptr in C++ is undefined behavior and shall never be done. But this is a standard specification of a couple of languages (which happen to be used for writing OSes) and not a universal hardware constraint. The OP seems aware of all this and asks what value there would be if he could access it. Simple intellectual curiosity :-) – Christophe Dec 11 '16 at 10:46
5

This is just hardware dependent. The address 0 will be valid in quite a number of processors. It's an address like any other address. So whether your CPU will raise an exception on accessing the address 0 just depends. I just checked with the 6502 (which still is used in washing machines) where the zero-page is an extra register bank. Other processors map this page to I/O registers.

Interestingly the idea to use hardware traps for 0-pointer recognition seems to be a rather "new" idea: see ACM paper

3

I know that long time ago computer scientists decided to treat all pointers to memory cell of address 0 as NULL.

Sort of, and practically speaking, yes. However, going back to the C language, in the days of yore, the literal number 0 is taken to mean the null value in a pointer context. Yet the standard does not require that null has a bit pattern value of all zeros. It could, for example, as an implementation choice, be -1 instead (the bit pattern of all ones). However, over the years, implementers have settled on the bit pattern of all zeroes to mean 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?

You would have to decide what context your asking about: directly on the processor, or in a process on an operating system.

On an embedded controller/processor, you might not have an operating system with virtual memory. And further, it is common for microprocessors to not only have memory at zero (and/or in highmem) that relates to I/O devices, or is otherwise special rather than being true memory, though true memory is also commonly found at address zero. So in this environment, zero exists as an address, and may or may not be special to the processor in some way.

For a process running within an operating system that offers virtual memory, modern operating systems will often choose to map a number of pages around zero effectively to undefined, so there simply isn't any actual memory at address zero. A request to access memory at zero will result in a fault (such as a segmentation fault).

On the other hand, some operating systems will provide readonly pages of zeros and allow reads to or around zero, while only faulting on writes to zero.

  • We're mostly using a flat memory model nowadays, so a value of 0 for a null-pointer usually makes sense. But back when C was designed, that wasn't true. Different pointers could have different physical structure (e.g. a near pointer could take 8 bits, while a long pointer could take 16 bits), or you could have segmented memory of some kind (so a null pointer would really be e.g. a valid pointer to the null segment)... the cases were many, and the "physical" value of NUL often wasn't zero (in C, the logical NUL == 0 was translated to the actual "null pointer" value during compilation). – Luaan Sep 13 '17 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.