Can a pointer (address) ever be negative?

前端 未结 13 1685
情书的邮戳
情书的邮戳 2020-11-29 05:53

I have a function that I would like to be able to return special values for failure and uninitialized (it returns a pointer on success).

Currently i

13条回答
  •  渐次进展
    2020-11-29 06:15

    You don't need to care about the signness of a pointer, because it's implementation defined. The real question here is "how to return special values from a function returning pointer?" which I've explained in detail in my answer to the question Pointer address span on various platforms

    In summary, the all-one bit pattern (-1) is (almost) always safe, because it's already at the end of the spectrum and data cannot be stored wrapped around to the first address, and the malloc family never returns -1. In fact this value is even returned by many Linux system calls and Win32 APIs to indicate another state for the pointer. So if you need just failure and uninitialized then it's a good choice

    But you can return far more error states by utilizing the fact that variables must be aligned properly (unless you specified some other options). For example in a pointer to int32_t the low 2 bits are always zero which means only ¹⁄₄ of the possible values are valid addresses, leaving all of the remaining bit patterns for you to use. So a simple solution would be just checking the lowest bit

    int* result = func();
    if (!result)
        error_happened();
    else if ((uintptr_t)result & 1)
        uninitialized();
    

    In this case you can return both a valid pointer and some additional data at the same time

    You can also use the high bits for storing data in 64-bit systems. On ARM there's a flag that tells the CPU to ignore the high bits in the addresses. On x86 there isn't a similar thing but you can still use those bits as long as you make it canonical before dereferencing. See Using the extra 16 bits in 64-bit pointers

    See also

    • Is ((void *) -1) a valid address?

提交回复
热议问题