2 ms·
In C, a "null pointer" (NULL, (void *)0, etc.) does not necessarily correspond to an address with all bits zero.[1] The compiler translates any pointer constant
by msarnoff 15y ago
In C, a "null pointer" (NULL, (void *)0, etc.) does not necessarily correspond to an address with all bits zero.[1] The compiler translates any pointer constants with a value of 0 to an invalid address appropriate for that machine.
I haven't verified this, but I could see this being used by microcontroller compilers, where address 0x0000 is often a memory-mapped register.
[1] http://c-faq.com/null/index.html http://c-faq.com/null/index.html
- dkersten 14y agoAlso the C null pointer on modern system is the logical (virtual memory) address which is not necessarily mapped to 0x0 physical address - in fact, to trap the null pointer access, this page would be left unmapped so the OS receives a page fault. This means that before virtual memory is set up (or if a page is mapped for logical address 0x0), 0x0 is a perfectly valid memory location, both for data storage and executing instructions from. Basically, the null pointer "error" is trapped by the OS at a higher level.
- scott_s 14y agoIn fact, there's an entire class of kernel exploits where if you know a certain kind of behavior will cause the kernel to dereference 0x0, and that system allows you to mmap the lowest page in your virtual address space, you can take over the machine. See https://blogs.oracle.com/ksplice/entry/much_ado_about_null_an1 https://blogs.oracle.com/ksplice/entry/much_ado_about_null_a... and https://blogs.oracle.com/ksplice/entry/much_ado_about_null_exploiting1 https://blogs.oracle.com/ksplice/entry/much_ado_about_null_e... (dkersten, I assume you know this, I'm pointing it out for the benefit of others.)