sizeof (int) == sizeof (void*)?

后端 未结 10 921
有刺的猬
有刺的猬 2020-12-03 01:50

Is there an integer type with the same size as pointer? Guaranteed on all microarchitectures?

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 02:26

    The C99 standard defines standard int types:

    7.18.1.4 Integer types capable of holding object pointers The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

        intptr_t
    

    The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

       uintptr_t
    

    These types are optional.

    C99 also defines size_t and ptrdiff_t:

    The types are

      ptrdiff_t
    

    which is the signed integer type of the result of subtracting two pointers;

      size_t
    

    which is the unsigned integer type of the result of the sizeof operator; and

    The architectures I've seen have the maximum size of an object equal to the whole memory, so sizeof(size_t) == sizeof(void*), but I'm not aware of anything that is both portable to C89 ( which size_t is ) and guaranteed to be large enough ( which uintptr_t is ).

提交回复
热议问题