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)
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. NoteThis pragma is a GNU compiler extension that the ARM compiler supports. Syntax#pragma pack(n) Where: nis the alignment in bytes, valid alignment values being1,2,4and8. DefaultThe default is#pragma pack(8). ErrorsTaking 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. ExamplesThis example demonstrates howpack(2)aligns integer variablebto a 2-byte boundary. The layout ofSis: Figure 9-1 Nonpacked structure S
The layout ofSPis: Figure 9-2 Packed structure SP
NoteIn 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 |
1111.png (9.59 KB, 下载次数: 29)