C语言 __attribute__(packed) v/s GCC __attribute__((aligned(x))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14332633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:02:52  来源:igfitidea点击:

__attribute__(packed) v/s GCC __attribute__((aligned(x))

cgcc

提问by Katoch

Following GCC __attribute__(packed )will pack to byte boundary, aligned is used for which purpose :--

以下 GCC__attribute__(packed )将打包到字节边界,对齐用于以下目的:--

u8 rx_buf[14] __attribute__((aligned(8)));  
struct S { short f[3]; } __attribute__ ((aligned (8)));

above array will be of 16 byte, am I right.

上面的数组将是 16 字节,我是对的。

means sizeof(rx_buff)will be 16 byte .. i.e 2 byte alignment at end

意味着sizeof(rx_buff)将是 16 字节 .. 即末尾的 2 字节对齐

回答by Michael Foukarakis

The answer to your question is no. The alignedattribute does not change the sizes of variables it is applied to, but the situation is slightly different for structure members. To quote the manual,

你的问题的答案是否定的。该aligned属性不会改变它所应用的变量的大小,但结构成员的情况略有不同。引用手册

aligned (alignment)

This attribute specifies a minimum alignment for the variable or structure field, measured in bytes. For example, the declaration:

int x __attribute__ ((aligned (16))) = 0;

causes the compiler to allocate the global variable x on a 16-byte boundary.

对齐(对齐)

此属性指定变量或结构字段的最小对齐方式,以字节为单位。例如,声明:

int x __attribute__ ((aligned (16))) = 0;

导致编译器在 16 字节边界上分配全局变量 x。

and,

和,

packed

The packed attribute specifies that a variable or structure field should have the smallest possible alignment — one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.

Here is a structure in which the field x is packed, so that it immediately follows a:

struct foo
{
    char a;
    int x[2] __attribute__ ((packed));
};

打包

打包属性指定变量或结构字段应具有尽可能小的对齐方式——一个字节用于变量,一位用于字段,除非您使用对齐属性指定更大的值。

这是一个结构体,其中字段 x 被打包,因此它紧跟在 a 之后:

struct foo
{
    char a;
    int x[2] __attribute__ ((packed));
};

Note that the alignedattribute may change the memory layout of structures, by inserting padding between members. Subsequently, the size of the structure will change. For instance:

请注意,该aligned属性可能会通过在成员之间插入填充来更改结构的内存布局。随后,结构的大小将发生变化。例如:

struct t {
    uint8_t  a __attribute__((aligned(16))); /* one byte plus 15 as padding here */
    uint16_t b __attribute__((aligned(16)));
};

would lead to 15 bytes of padding after awhereas the default alignment for the target architecture might have resulted in less. If you specified the packedattribute for the structure andlost the alignedattributes the structure would have a size of 3 bytes. Here's an illustration of how the memory layout of a structure mightlook like in each case.

之后会导致 15 个字节的填充,a而目标体系结构的默认对齐可能会导致更少。如果您packed为结构指定了属性丢失了aligned属性,则该结构的大小将为 3 个字节。下面是一个结构的存储器布局如何插图可能看起来像在每种情况下。

struct twith no attributes and default alignment on 8-byte boundary:

struct t在 8 字节边界上没有属性和默认对齐方式:

+-+-------+--+-------+
|a|       |bb|       |
+-+-------+--+-------+

struct twhen a and b are aligned on 16-byte boundaries:

struct t当 a 和 b 在 16 字节边界上对齐时:

+-+---------------+--+---------------+
|a|    padding    |bb|    padding    |
+-+---------------+--+---------------+

struct twhen a and b have no alignment restrictions and t is packed:

struct t当 a 和 b 没有对齐限制并且 t 被打包时:

+-+--+
|a|bb|
+-+--+

回答by duskwuff -inactive-

above array will be of 16 byte, am I right.

上面的数组将是 16 字节,我是对的。

Incorrect. The array is still 14 bytes long; all that __attribute__((aligned))does is provide any necessary padding outside the arrayto align it to an 8-byte boundary. It is impossible to safely assume anything about where this padding exists, or how much of it there is.

不正确。数组仍然是 14 字节长;所有__attribute__((aligned))的作用是提供任何必要的填充阵列外部,将其调整到一个8字节的边界。不可能安全地假设有关此填充的位置或其中有多少的任何信息。

As such, sizeof(rx_buf)will remain 14, just as it would have been without the alignment.

因此,sizeof(rx_buf)将保持为 14,就像没有对齐一样。