“#pragma pack”的区别是什么?和“__属性__((对齐))” [英] What's the difference between "#pragma pack" and "__attribute__((aligned))"

查看:134
本文介绍了“#pragma pack”的区别是什么?和“__属性__((对齐))”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

  #pragma pack(L1_CACHE_LINE)
struct A {
// .. 。
};
#pragma pack()

A a;

  struct A {
// ...
};

a __attritube __((aligned(L1_CACHE_LINE)))

有什么区别在thwm之间?



感谢提前!

解决方案

#pragma pack (字节对齐)会影响由字节对齐输入指定的结构的每个成员或其自然对齐边界上的较小者。
$ b $ __属性__((aligned(byte-alignment)))会影响变量(或结构体字段,如果在struct中指定的话)



我相信以下内容是相同的

  #define L1_CACHE_LINE 2 
$ b $ struct A
{
u_int32_t a __attribute__((aligned(L1_CACHE_LINE)));
u_int32_t b __attribute__((aligned(L1_CACHE_LINE)));
u_int16_t c __attribute__((aligned(L1_CACHE_LINE)));
u_int16_t d __attribute__((aligned(L1_CACHE_LINE)));
u_int32_t e __attribute__((aligned(L1_CACHE_LINE)));
};


#pragma pack(L1_CACHE_LINE)
struct A
{
u_int32_t a;
u_int32_t b;
u_int16_t c;
u_int16_t d;
u_int32_t e;
};
#pragma pack()

其中A a __attritube __( (对齐的(L1_CACHE_LINE)))将保证结构A 内的 u_int32_t a 2字节,但不会以相同的方式对齐其他变量。



参考:


  1. http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Fcompiler%2Fref%2Frnpgpack.htm

  2. http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/attributes-variables.html


Eg:

#pragma pack(L1_CACHE_LINE)
struct A {
  //...
};
#pragma pack()

A a;

and

struct A {
  //...
};

A a __attritube__((aligned(L1_CACHE_LINE)))

What's difference between thwm?

Thanks advance!

解决方案

The #pragma pack(byte-alignment) effect each member of the struct as specified by the byte-alignment input, or on their natural alignment boundary, whichever is less.

The __attribute__((aligned(byte-alignment))) affect the minimum alignment of the variable (or struct field if specified within the struct)

I believe the following are equivalent

#define L1_CACHE_LINE 2

struct A
{
    u_int32_t   a   __attribute__ ( (aligned(L1_CACHE_LINE)) );
    u_int32_t   b   __attribute__ ( (aligned(L1_CACHE_LINE)) );
    u_int16_t   c   __attribute__ ( (aligned(L1_CACHE_LINE)) );       
    u_int16_t   d   __attribute__ ( (aligned(L1_CACHE_LINE)) );      
    u_int32_t   e   __attribute__ ( (aligned(L1_CACHE_LINE)) );     
};


#pragma pack(L1_CACHE_LINE)
struct A
{
    u_int32_t   a;  
    u_int32_t   b;  
    u_int16_t   c;  
    u_int16_t   d;  
    u_int32_t   e;  
};
#pragma pack()

where is A a __attritube__((aligned(L1_CACHE_LINE))) will insure u_int32_t a inside struct A will aligned with 2 byte but will not align the other variable in the same manner.

Reference:

  1. http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Fcompiler%2Fref%2Frnpgpack.htm
  2. http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/attributes-variables.html

这篇关于“#pragma pack”的区别是什么?和“__属性__((对齐))”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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