字节对齐__align(),__attribute((aligned (n))),#pragma pack(n)出处来源

http://www.openedv.com/posts/list/11308.htm

虽然帖子很久了,但是我还是说下。
MDK5环境下,详见MDK--HELP--uVision help----Compiler User Guide------compiler specific Features----9.2__align、9.6__attribute__((aligned))、9.91#pragma pack(n),解释的很清晰。我依次贴出来。

 

虽然在MDK4中没找到,但是知道了方法 是编译器所使用,可能版本不同,所在目录不同。也是在类似编译器相关文件中找到也可以通过搜索查找到如下图

MDK--HELP--uVision help---RealView Compiler Reference Guide-----compiler specific Features----,解释的很清晰。我依次贴出来。

9.2 __align

The__alignkeyword instructs the compiler to align a variable on ann-byte boundary.

__alignis a storage class modifier. It does not affect the type of the function.

Syntax

__align(n)

Where:

n

is the alignment boundary.

For local variables,ncan take the values 1, 2, 4, or 8.

For global variables,ncan take any value up to 0x80000000 in powers of 2.

Usage

__align(n)is useful when the normal alignment of the variable being declared is less thann. Eight-byte alignment can give a significant performance advantage with VFP instructions.

__aligncan be used in conjunction withexternandstatic.

Restrictions

Because__alignis a storage class modifier, it cannot be used on:

  • Types, includingtypedefs and structure definitions.

  • Function parameters.

You can only overalign. That is, you can make a two-byte object four-byte aligned but you cannot align a four-byte object at 2 bytes.

Examples

__align(8) char buffer[128];  // buffer starts on eight-byte boundary
[/mw_shl_code]
void foo(void) { ... __align(16) int i; // this alignment value is not permitted for // a local variable ... } __align(16) int i; // permitted as a global variable. [/mw_shl_code] 

Related reference

7.104 --min_array_alignment=opt

9.60 __attribute__((aligned)) variable attribute

#pragma pack(n)

Non-Confidential ARM DUI0375E
ARM® Compiler v5.04 for ?Vision armcc User GuideVersion 5
Home > Compiler-specific Features > #pragma pack(n)

9.91 #pragma pack(n)

This pragma aligns members of a structure to the minimum ofnand their natural alignment. Packed objects are read and written using unaligned accesses.

Note

This pragma is a GNU compiler extension that the ARM compiler supports.

Syntax

#pragma pack(n)

Where:

n

is the alignment in bytes, valid alignment values being1,2,4and8.

Default

The default is#pragma pack(8).

Errors

Taking the address of a field in a#pragma packedstructdoes not yield a__packedpointer, so the compiler does not produce an error if you assign this address to a non-__packedpointer. However, the field might not be properly aligned for its type, and dereferencing such an unaligned pointer results in undefined behavior.

Examples

This example demonstrates howpack(2)aligns integer variablebto a 2-byte boundary.

typedef struct
{ 
    char a;
    int b;
} S;
#pragma pack(2)
typedef struct
{ 
    char a;
    int b;
} SP;
S var = { 0x11, 0x44444444 };
SP pvar = { 0x11, 0x44444444 };
[/mw_shl_code]
				

The layout ofSis:

Figure 9-1 Nonpacked structure S

 

 

The layout ofSPis:

Figure 9-2 Packed structure SP

 

 

Note

In this layout,xdenotes one byte of padding.

SPis a 6-byte structure. There is no padding afterb.

Related concepts

4.34 The __packed qualifier and unaligned data access in C and C++ code

4.39 Comparisons of an unpacked struct, a __packed struct, and a struct with individually __packed fields, and of a __packed struct and a #pragma packed struct

1111.png (9.59 KB, 下载次数: 29)

 

1111.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值