-6

As I know the memory location 0x00000000 isn't accessible through a pointer in C, but recently in my project I was able to access the zeroth location using a pointer. The the zeroth location is flash memory. But according to the post Why is address zero used for the null pointer? , referencing zero through pointer is a reserved value. So, my question is as it is a reserved value why this doesn't hold true for memory mapped to flash at location zero?

Processor : TMS470Rxx

OS : Micro C OS-II

Thanks

Community
  • 1
  • 1
geek
  • 77
  • 3
  • 12
  • 1
    You're asking us why you experienced particular platform-specific behavior on some platform, but you've told us *nothing* about that platform. What CPU? What OS? Also, how do you know that the reason is that the zeroth location is flash? If you know something we don't, please share it because otherwise, we'll have no hope of figuring out this mystery with even fewer clues than you have. – David Schwartz Nov 20 '15 at 07:54
  • 1
    0x0 is a reserved value from the C point of view, but if your platform could access it you could see that address. Eg. MCUs , like PIC16 or Cortex M, at address 0 store the iPC, at address 0x4 store ISP and you can point and read it. – LPs Nov 20 '15 at 07:55
  • 1
    You might also want to check [Could I ever want to access the address zero?](http://stackoverflow.com/questions/2761360/could-i-ever-want-to-access-the-address-zero?lq=1) – Bo Persson Nov 20 '15 at 07:55
  • 1
    You really should clarify whether this a C question, a C++ question, or somehow involves both languages. Because the rules for `NULL` are very different, and your question seems to be specifically based on what you know about C pointers. (Which, by the way, is incorrect. Though almost correct for C++.) – David Schwartz Nov 20 '15 at 07:58
  • There's a huge difference between whether you're allowed to do something and whether it's possible to do it. You should also read the question you linked to and its answers until you understand the difference between "address zero" and "null pointer". – molbdnilo Nov 20 '15 at 08:59

2 Answers2

2

There are many platforms where address zero behaves differently than on the typical desktop PC.

There are microcontrollers where hardware devices are memory-mapped at device zero.

There are operating systems that will happily let you specifically map something at address zero if you wish to. If you have writable flash mapped there, then you can write there.

Different platforms work in different ways.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

The decision as to what happens when using what address is rather complex. It depends on the processor architecture, OS and sometimes "what some software does".

The processor may not have memory protection, or address zero may indeed be "used for something" (in x86 real-mode, that DOS runs in, address zero contains the vector table, where interrupts and such jump to, so it's incredibly sensitive to being overwritten - hence badly written programs could and would crash not just themselves but the entire machine in DOS).

Typically, modern OS's on processors that have "virtual memory mapping" (so the physical address that the processor actually uses is not (or may not be) the same as the virtual address that the program sees) will map address zero as "not accessible" for your typical applications.

The OS may allow access to address zero at times: I had a bug in a Windows driver many years ago, that used a NULL-pointer, and that that particular situation, address 0 was not causing a crash. The crash happened much later when the content of address zero was being used for something - at which point the system blue-screened (at least until I attached the debugger and debugged the problem, and then fixed it so it didn't try to use a NULL pointer - I don't remember if I had to allocate some memory or just skipped that bit if the pointer was NULL).

The choice of 0 for NULL is based on a combination of "we have to choose some value (and there is not one value that can be 100% sure that nobody will ever need to use)" - particularly on machines with a 16 or 32-bit address range. Normally, however, address zero, if it is used, contains something "special" (vector table for interrupts, bootstrap code for the processor, or similar), so you are unlikely to meaningfully store data in there. C and C++ as languages do not require that NULL-pointers can't be accessed [but also does not "allow you" to freely access this location], just that this value can be used as "pointer that doesn't point at anything" - and the spec also provides for the case where your NULL value is not ACTUALLY zero. But the compiler has to deal with "ZERO means NULL for pointers, so replace it with X".

The value zero does, at least sometimes, have a benefit over other values - mostly that many processors have special instructions to compare with or identify zero.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Many POSIX system functions like `mmap()` and `shmat()` also return -1 for an invalid pointer instead of NULL because [sometimes 0 is a valid memory address for them](https://stackoverflow.com/q/24562691/995714) – phuclv Oct 09 '20 at 03:23