为什么只有“char"的结构中没有填充?类型成员? [英] Why is there no padding in the structure for only "char" type members?

查看:19
本文介绍了为什么只有“char"的结构中没有填充?类型成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在结构中只声明了 char 类型的成员.

I have declared only 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
", sizeof(s));
    return 0;
}

输出: [现场演示]

5

那么,为什么结构中只有 char 类型的成员没有填充?

So, why is there no padding in the structure for only char type members?

推荐答案

结构中的填充存在(大部分)以强制单个成员与其基本对齐要求,即(C11 3.2p1):

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

结构中间和末尾的填充用于确保即使在这些结构的数组中,每个成员仍将根据其对齐要求对齐.C11 6.2.8p1:

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:

完整的对象类型具有对齐要求,这对可以分配该类型对象的地址进行了限制.对齐是实现定义的整数值,表示可以分配给定对象的连续地址之间的字节数.对象类型对该类型的每个对象都有对齐要求:可以使用 _Alignas 关键字请求更严格的对齐.

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.

现在,所有其他类型的对齐要求是实现定义的,但隐含一件事:因为对齐要求以 size_t 表示;sizeof (char) 为 1,指向字符类型的指针可用于寻址其他类型中的每个单独字符,字符类型的基本对齐要求不能超过 1.令人惊讶的是,这不是拼写完全不符合 C 标准;它只是有这个模糊的措辞(C11 6.2.8p6):

Now, the alignment requirement of every other type is implementation-defined, but one thing is implied: since the alignment requirement is expressed in 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):

charsigned charunsigned char 类型应具有最弱的对齐要求.

The types char, signed char, and unsigned char shall have the weakest alignment requirement.

由于 char 的对齐方式最多为 1,编译器不需要添加任何填充,因为即使结构正好是 5 个字节长,那么即使在一个数组,有些结构从奇数地址开始,这些结构的每个成员仍然会正确对齐.

As the alignment of 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.

这篇关于为什么只有“char"的结构中没有填充?类型成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆