Could I ever want to access the address zero?

前端 未结 17 2881
花落未央
花落未央 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:23

    The compiler takes care of this for you (comp.lang.c FAQ):

    If a machine uses a nonzero bit pattern for null pointers, it is the compiler's responsibility to generate it when the programmer requests, by writing "0" or "NULL," a null pointer. Therefore, #defining NULL as 0 on a machine for which internal null pointers are nonzero is as valid as on any other, because the compiler must (and can) still generate the machine's correct null pointers in response to unadorned 0's seen in pointer contexts.

    You can get to address zero by referencing zero from a non-pointer context.

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

    On the x86, address 0 (or rather, 0000:0000) and its vicinity in real mode is the location of the interrupt vector. In the bad old days, you would typically write values to the interrupt vector to install interrupt handers (or if you were more disciplined, used the MS-DOS service 0x25). C compilers for MS-DOS defined a far pointer type which when assigned NULL or 0 would recieve the bit pattern 0000 in its segment part and 0000 in its offset part.

    Of course, a misbehaving program that accidentally wrote to a far pointer whose value was 0000:0000 would cause very bad things to happen on the machine, typically locking it up and forcing a reboot.

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

    In practice, C compilers will happily let your program attempt to write to address 0. Checking every pointer operation at run time for a NULL pointer would be a tad expensive. On computers, the program will crash because the operating system forbids it. On embedded systems without memory protection, the program will indeed write to address 0 which will often crash the whole system.

    The address 0 might be useful on an embedded systems (a general term for a CPU that's not in a computer; they run everything from your stereo to your digital camera). Usually, the systems are designed so that you wouldn't need to write to address 0. In every case I know of, it's some kind of special address. Even if the programmer needs to write to it (e.g., to set up an interrupt table), they would only need to write to it during the initial boot sequence (usually a short bit of assembly language to set up the environment for C).

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

    I compiled some code using gcc for the Motorola HC11, which has no MMU and 0 is a perfectly good address, and was disappointed to find out that to write to address 0, you just write to it. There's no difference between NULL and address 0.

    And I can see why. I mean, it's not really possible to define a unique NULL on an architecture where every memory location is potentially valid, so I guess the gcc authors just said 0 was good enough for NULL whether it's a valid address or not.

          char *null = 0;
    ; Clears 8-bit AR and BR and stores it as a 16-bit pointer on the stack.
    ; The stack pointer, ironically, is stored at address 0.
    1b:   4f              clra
    1c:   5f              clrb
    1d:   de 00           ldx     *0 <main>
    1f:   ed 05           std     5,x
    

    When I compare it with another pointer, the compiler generates a regular comparison. Meaning that it in no way considers char *null = 0 to be a special NULL pointer, and in fact a pointer to address 0 and a "NULL" pointer will be equal.

    ; addr is a pointer stored at 7,x (offset of 7 from the address in XR) and 
    ; the "NULL" pointer is at 5,y (offset of 5 from the address in YR).  It doesn't
    ; treat the so-called NULL pointer as a special pointer, which is not standards
    ; compliant as far as I know.
    37:   de 00           ldx     *0 <main>
    39:   ec 07           ldd     7,x
    3b:   18 de 00        ldy     *0 <main>
    3e:   cd a3 05        cpd     5,y
    41:   26 10           bne     53 <.LM7>
    

    So to address the original question, I guess my answer is to check your compiler implementation and find out whether they even bothered to implement a unique-value NULL. If not, you don't have to worry about it. ;)

    (Of course this answer is not standard compliant.)

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

    I have at times used loads from address zero (on a known platform where that would be guaranteed to segfault) to deliberately crash at an informatively named symbol in library code if the user violates some necessary condition and there isn't any good way to throw an exception available to me. "Segfault at someFunction$xWasnt16ByteAligned" is a pretty effective error message to alert someone to what they did wrong and how to fix it. That said, I wouldn't recommend making a habit of that sort of thing.

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