Could I ever want to access the address zero?

前端 未结 17 2880
花落未央
花落未央 2020-11-29 00:38

The constant 0 is used as the null pointer in C and C++. But as in the question \"Pointer to a specific fixed address\" there seems to be some possible use of assigning fixe

相关标签:
17条回答
  • 2020-11-29 01:05

    On a tangential note: you might be interested to know that with Microsoft's C++ compiler, a NULL pointer to member will be represented as the bit pattern 0xFFFFFFFF on a 32-bit machine. That is:

    struct foo
    {
          int field;
    };
    
    int foo::*pmember = 0;     // 'null' member pointer
    

    pmember will have the bit pattern 'all ones'. This is because you need this value to distinguish it from

    int foo::*pmember = &foo::field;
    

    where the bit pattern will indeed by 'all zeroes' -- since we want offset 0 into the structure foo.

    Other C++ compilers may choose a different bit pattern for a null pointer to member, but the key observation is that it won't be the all-zeroes bit pattern you might have been expecting.

    0 讨论(0)
  • 2020-11-29 01:06

    If I remember correctly, in an AVR microcontroller the register file is mapped into an address space of RAM and register R0 is at the address 0x00. It was clearly done in purpose and apparently Atmel thinks there are situations, when it's convenient to access address 0x00 instead of writing R0 explicitly.

    In the program memory, at the address 0x0000 there is a reset interrupt vector and again this address is clearly intended to be accessed when programming the chip.

    0 讨论(0)
  • 2020-11-29 01:07

    It may surprise many people, but in the core C language there is no such thing as a special null pointer. You are totally free to read and write to address 0 if it's physically possible.

    The code below does not even compile, as NULL is not defined:

    int main(int argc, char *argv[])
    {
        void *p = NULL;
        return 0;
    }
    

    OTOH, the code below compiles, and you can read and write address 0, if the hardware/OS allows:

    int main(int argc, char *argv[])
    {
        int *p = 0;
        *p = 42;
        int x = *p; /* let's assume C99 */
    }
    

    Please note, I did not include anything in the above examples. If we start including stuff from the standard C library, NULL becomes magically defined. As far as I remember it comes from string.h.

    NULL is still not a core C feature, it's a CONVENTION of many C library functions to indicate the invalidity of pointers. The C library on the given platform will define NULL to a memory location which is not accessible anyway. Let's try it on a Linux PC:

    #include <stdio.h>
    int main(int argc, char *argv[])
    {
            int *p = NULL;
            printf("NULL is address %p\n", p);
            printf("Contents of address NULL is %d\n", *p);
            return 0;
    }
    

    The result is:

    NULL is address 0x0
    Segmentation fault (core dumped)
    

    So our C library defines NULL to address zero, which it turns out is inaccessible. But it was not the C compiler, of not even the C-library function printf() that handled the zero address specially. They all happily tried to work with it normally. It was the OS that detected a segmentation fault, when printf tried to read from address zero.

    0 讨论(0)
  • 2020-11-29 01:09

    Remember that in all normal cases, you don't actually see specific addresses. When you allocate memory, the OS supplies you with the address of that chunk of memory.

    When you take the reference of a variable, the the variable has already been allocated at an address determined by the system.

    So accessing address zero is not really a problem, because when you follow a pointer, you don't care what address it points to, only that it is valid:

    int* i = new int(); // suppose this returns a pointer to address zero
    *i = 42; // now we're accessing address zero, writing the value 42 to it
    

    So if you need to access address zero, it'll generally work just fine.

    The 0 == null thing only really becomes an issue if for some reason you're accessing physical memory directly. Perhaps you're writing an OS kernel or something like that yourself. In that case, you're going to be writing to specific memory addresses (especially those mapped to hardware registers), and so you might conceivably need to write to address zero. But then you're really bypassing C++ and relying on the specifics of your compiler and hardware platform.

    Of course, if you need to write to address zero, that is possible. Only the constant 0 represents a null pointer. The non-constant integer value zero will not, if assigned to a pointer, yield a null pointer.

    So you could simply do something like this:

    int i = 0;
    int* zeroaddr = (int*)i;
    

    now zeroaddr will point to address zero(*), but it will not, strictly speaking, be a null pointer, because the zero value was not constant.

    (*): that's not entirely true. The C++ standard only guarantees an "implementation-defined mapping" between integers and addresses. It could convert the 0 to address 0x1633de20` or any other address it likes. But the mapping is usually the intuitive and obvious one, where the integer 0 is mapped to the address zero)

    0 讨论(0)
  • 2020-11-29 01:14

    You're starting from a mistaken premise. When you assign an integer constant with the value 0 to a pointer, that becomes a null pointer constant. This does not, however, mean that a null pointer necessarily refers to address 0. Quite the contrary, the C and C++ standards are both very clear that a null pointer may refer to some address other than zero.

    What it comes down to is this: you do have to set aside an address that a null pointer would refer to -- but it can be essentially any address you choose. When you convert zero to a pointer, it has to refer to that chosen address -- but that's all that's really required. Just for example, if you decided that converting an integer to a point would mean adding 0x8000 to the integer, then the null pointer to would actually refer to address 0x8000 instead of address 0.

    It's also worth noting that dereferencing a null pointer results in undefined behavior. That means you can't do it in portable code, but it does not mean you can't do it at all. When you're writing code for small microcontrollers and such, it's fairly common to include some bits and pieces of code that aren't portable at all. Reading from one address may give you the value from some sensor, while writing to the same address could activate a stepper motor (just for example). The next device (even using exactly the same processor) might be connected up so both of those addresses referred to normal RAM instead.

    Even if a null pointer does refer to address 0, that doesn't prevent you from using it to read and/or write whatever happens to be at that address -- it just prevents you from doing so portably -- but that doesn't really matter a whole lot. The only reason address zero would normally be important would be if it was decoded to connect to something other than normal storage, so you probably can't use it entirely portably anyway.

    0 讨论(0)
  • 2020-11-29 01:17

    Memory address 0 is also called the Zero Page. This is populated by the BIOS, and contains information about the hardware running on your system. All modern kernels protect this region of memory. You should never need to access this memory, but if you want to you need to do it from within kernel land, a kernel module will do the trick.

    0 讨论(0)
提交回复
热议问题