Are all data pointers the same size in one platform for all data types?

前端 未结 8 865
太阳男子
太阳男子 2020-11-22 16:33

Are char*, int*, long* or even long long* of same size (on a given platform)?

相关标签:
8条回答
  • 2020-11-22 17:23

    There is no such guarantee in either C or C++ ISO standards, but in practice, I've yet to see a platform where this doesn't hold.

    Note that regardless of this, reinterpret_cast'ing one pointer to another will more often than not lead to U.B., with a few exceptions (void*, and unsigned char* for PODs). So would any union tricks. So the obvious question is: why would you care?

    0 讨论(0)
  • 2020-11-22 17:25

    They're not guaranteed to be the same size, although on the platforms I have experience with they usually are.

    C 2011 online draft:

    6.2.5 Types
    ...
    28     A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
    48) The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
    0 讨论(0)
提交回复
热议问题