“__attribute__((packed,aligned(4)))"是什么意思? [英] What is the meaning of "__attribute__((packed, aligned(4))) "

查看:27
本文介绍了“__attribute__((packed,aligned(4)))"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是C语言,写成:

typedef struct __attribute__((packed, aligned(4))) Ball {
    float2 delta;
    float2 position;
    //float3 color;
    float size;
    //int arcID;
    //float arcStr;
} Ball_t;
Ball_t *balls;

请告诉我它是什么意思,以及如何使用这个关键字.

Please tell me what is the meaning of it,and how to use this keyword.

推荐答案

在回答之前,我想给你一些维基的数据

Before answering, I would like to give you some data from Wiki

数据结构对齐是数据在计算机内存中排列和访问的方式.它由两个独立但相关的问题组成:数据对齐数据结构填充.

Data structure alignment is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding.

当现代计算机读取或写入内存地址时,它将以字大小的块(例如 32 位系统上的 4 字节块)执行此操作.数据对齐是指将数据放在一个等于字长倍数的内存偏移量处,由于 CPU 处理内存的方式,这会提高系统的性能.

When a modern computer reads from or writes to a memory address, it will do this in word sized chunks (e.g. 4 byte chunks on a 32-bit system). Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory.

为了对齐数据,可能需要在上一个数据结构的结尾和下一个数据结构的开头之间插入一些无意义的字节,这就是数据结构填充.

To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.

gcc 提供了禁用结构填充的功能.即在某些情况下避免这些无意义的字节.考虑以下结构:

gcc provides functionality to disable structure padding. i.e to avoid these meaningless bytes in some cases. Consider the following structure:

typedef struct
{
     char Data1;
     int Data2;
     unsigned short Data3;
     char Data4;

}sSampleStruct;

sizeof(sSampleStruct) 将是 12 而不是 8.因为结构填充.默认情况下,在 X86 中,结构将填充为 4 字节对齐:

sizeof(sSampleStruct) will be 12 rather than 8. Because of structure padding. By default, In X86, structures will be padded to 4-byte alignment:

typedef struct
{
     char Data1;
     //3-Bytes Added here.
     int Data2;
     unsigned short Data3;
     char Data4;
     //1-byte Added here.

}sSampleStruct;

我们可以使用 __attribute__((packed,aligned(X))) 来坚持特定(X)大小的填充.X 应该是 2 的幂.请参阅此处

We can use __attribute__((packed, aligned(X))) to insist particular(X) sized padding. X should be powers of two. Refer here

typedef struct
{
     char Data1;
     int Data2;
     unsigned short Data3;
     char Data4;

}__attribute__((packed, aligned(1))) sSampleStruct;  

所以上面指定的 gcc 属性不允许结构填充.所以大小将为 8 个字节.

so the above specified gcc attribute does not allow the structure padding. so the size will be 8 bytes.

如果你想对所有的结构都做同样的事情,我们可以简单地使用#pragma

If you wish to do the same for all the structures, simply we can push the alignment value to stack using #pragma

#pragma pack(push, 1)

//Structure 1
......

//Structure 2
......

#pragma pack(pop)

这篇关于“__attribute__((packed,aligned(4)))"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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