问题
The C standard states (emphasize mine):
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.
Source: C11, §6.2.5/28
The wording of "the same representation and alignment" happens here often.
But what about the same size?
I wonder if there can be a difference between between these pointer objects in terms of the allocated size since the size of a pointer object can vary between the type pointed to even if the alignment and representation is the same.
Or in other words: Is there a guarantee that if the alignment and/or representation is equal, the size is so too?
Question:
- Is the size of an object equivalent to the size of another based upon the same alignment and/or representation?
Annotations:
The question is not specific to pointer objects only. The pointer guidance was just a reference to my mindset as it is a good example.
Citations from the standard are highly appreciated. Accepted answer must have quotations from the standard.
Setting is the exact same specific implementation. I don't talk about various alignments/representations/sizes of objects between different implementations.
Related (regarding the pointer example):
Are there any platforms where pointers to different types have different sizes?
Is the sizeof(some pointer) always equal to four?
Does the size of pointers vary in C?
回答1:
C 2018 clauses 6.2.6, entitled “Representations of types,” specifies representations of types. Paragraph 2 says:
Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined.
From this, it is clear that a representation of an object is a sequence of bytes, and that sequence has some number of bytes, some order, and some encoding. So the number of bytes, the order of the bytes, and the encoding of the bytes is part of the representation. Therefore, if two objects have the same representation, they have the same number of bytes, the same order, and the same encoding.
Since they have the same number of bytes, they have the same size.
As an example, if an object X is represented with bytes A, B, and C, and object Y is represented with bytes A, B, C, and D, then X and Y do not have the same representation.
回答2:
Two types having same presentation does not imply that the two types need to have same alignment requirements, though this is often true in practice.
The representation means that the similar object value is represented by the same bytes in the same order. Alignment tells what the address of the lowest byte in the type needs to be divisible by.
来源:https://stackoverflow.com/questions/62847600/is-the-size-of-an-object-equivalent-to-the-size-of-another-based-upon-the-same-a