Could I ever want to access the address zero?

前端 未结 17 3002
花落未央
花落未央 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.

提交回复
热议问题