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
179 views
in Technique[技术] by (71.8m points)

c - The sizeof and _Alignof value of a structure

Which of the following are true in Standard C?

(A) The sizeof a structure is equal to:

  1. the relative address of its last member plus the sizeof its last member. (I know this cannot be true.)

  2. the relative address of its last member plus the alignment value (as obtained by the _Alignof operator) of its last member. (This cannot be true also, because there are cases where the sizeof a type may be larger than its _Alignof value. See long double in 32 bit Windows GCC: sizeof is 12, _Alignof is 4.)

  3. the relative address of its last member plus the alignment value of the structure itself. (This cannot be true also, as explained in the previous statement.)

  4. the relative address of its last member plus the maximum of the last member's size and the alignment value of the structure itself.

  5. something else.

By relative address I mean the distance between the starting byte of its last member and the starting byte of its first member (or the structure itself) which can be obtained with the offsetof macro like this: offsetof(struct st, last_member).


(B) The _Alignof value of a structure is equal to:

  1. the _Alignof value of its member with the largest _Alignof value.

  2. something else.


Notes:

  • I am not talking about specific implementations on specific environments, but rather how a "Stardard C (C18)"-compliant implementation should behave theoretically.

  • _Alignof is the standard C operator and alignof is its macro synonym defined in stdalign.h header.

question from:https://stackoverflow.com/questions/65847914/the-sizeof-and-alignof-value-of-a-structure

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

1 Reply

0 votes
by (71.8m points)

The answer for A is “something else.”

The size of a structure must be at least the offset of its last member plus the size of that member plus enough padding bytes to make the structure’s size a multiple of its alignment requirement (which equals the largest alignment requirement of any of its members). A C implementation may add additional padding bytes in multiples of the alignment requirement, although I know of none that do.

The answer for B is the alignment requirement of the structure (its _Alignof value) equals strictest alignment requirement of its members (the largest _Alignof value of them).

In particular, note that C 2018 6.2.7 4 says “… Every valid alignment value shall be a nonnegative integral power of two.” Thus, if any alignment satisfies the strictest member alignment requirement, it satisfies each member alignment requirement, so it is sufficient.


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

...