Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

c - Is the size of an object equivalent to the size of another based upon the same alignment and/or representation?

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):

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...