为什么在结构中没有填充只有“字符”类型成员? [英] Why is there no padding in the structure for only "char" type members?
问题描述
我在结构中只声明了 char
类型成员。
#include< stdio.h>
struct st
{
char c1;
char c2;
char c3;
char c4;
char c5;
};
int main(){
struct st s;
printf(%zu\\\
,sizeof(s));
返回0;
}
输出: [现场演示]
5
那么,为什么没有填充结构中只有 char
类型成员?
结构中的填充(主要)强制个别 em>会员符合他们的基本对准要求,即( C11 3.2p1 ):
要求特定类型的对象位于具有地址的存储边界上是字节地址的特定倍数
结构中间和结尾处的填充用于确保即使在这些结构的数组中,每个成员仍然会根据其对齐要求对齐。 C11 6.2.8p1 :
完整的对象类型具有对齐要求,这些对齐要求限制了可以分配该类型对象的地址。对齐是实现定义的整数值,表示可以分配给定对象的连续地址之间的字节数。对象类型对该类型的每个对象强加一个对齐要求:可以使用_Alignas关键字请求更严格的对齐。
现在,每个其他类型的对齐要求都是实现定义的,但有一点是隐含的:因为对齐要求表达为为size_t
; sizeof(char)
是1,并且可以使用指向字符类型的指针来处理其他类型中的每个单独字符,字符类型不能具有超过1的基本对齐要求令人惊讶的是,这在C标准中没有详细说明;它只是有这个模糊的措辞( C11 6.2.8p6
$ b
类型
char
,signed char
和无符号字符
应该具有最弱的对齐要求。
由于 I have declared only Output: [Live Demo] So, why is there no padding in the structure for only The padding in structure exist (mostly) to enforce that the individual members are aligned to their fundamental alignment requirement, i.e. (C11 3.2p1): requirement that objects of a particular type be located on storage boundaries with addresses that are particular multiples of a byte address The padding in the middle and at the end of the structure is used to ensure that even within an array of these structures, each member will be still aligned according to their alignment requirement. C11 6.2.8p1: Complete object types have alignment requirements which place restrictions on the addresses at which objects of that type may be allocated. An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated. An object type imposes an alignment requirement on every object of that type: stricter alignment can be requested using the _Alignas keyword. Now, the alignment requirement of every other type is implementation-defined, but one thing is implied: since the alignment requirement is expressed in The types As the alignment of 这篇关于为什么在结构中没有填充只有“字符”类型成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! char
的对齐最多为1,所以编译器不需要添加任何填充,因为即使结构长度恰好为5个字节,即使在一个数组中,一些结构从奇数地址开始,这些结构的每个成员仍然会正确对齐。 / p> char
type members in the structure.#include <stdio.h>
struct st
{
char c1;
char c2;
char c3;
char c4;
char c5;
};
int main() {
struct st s;
printf("%zu\n", sizeof(s));
return 0;
}
5
char
type members?
size_t
; sizeof (char)
is 1, and pointers to character types can be used to address each individual character in other types, a character type cannot have a fundamental alignment requirement of more than 1. Surprisingly this is not spelt out in the C standard at all; it just has this this vague wording (C11 6.2.8p6):
char
, signed char
, and unsigned char
shall have the weakest alignment requirement. char
is at most 1, the compiler need not add any padding, because even if the structure is exactly 5 bytes long, then even in an array, with some structures starting at odd address, each of the members of those structures would still be properly aligned.