海迹天涯 发表于 2017-2-15 09:56:54

C语言问题请教

今天看到一个新的关键字不能理解在这里的作用,图中红色方框内的代码,有没有大神能分析并解释下,感激不尽!

MrJiu 发表于 2017-2-15 10:21:17

你先右键跟踪一下,有可能是一个define定义的其他东西....或者特定编译器自己支持的关键字等等....总之就是右键先跟踪一下,这个东西是代表啥!!!

creep 发表于 2017-2-15 10:34:31

__ALIGN_BEGIN 和 __ALIGN_END 应该是 关于__align 的宏定义,说的都是字节对齐相关的定义:
9.2 __align
The __align keyword instructs the compiler to align a variable on an n-byte boundary.

__align is 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, n can take the values 1, 2, 4, or 8.
For global variables, n can 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 than n. Eight-byte alignment can give a significant performance advantage with VFP instructions.
__align can be used in conjunction with extern and static.
Restrictions
Because __align is a storage class modifier, it cannot be used on:
Types, including typedefs 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.
Example
__align(8) char buffer;// buffer starts on eight-byte boundary
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.

creep 发表于 2017-2-15 10:36:10

除了上面还有__attribute__((aligned)) 的用法

9.61 __attribute__((aligned)) variable attribute
The aligned variable attribute specifies a minimum alignment for the variable or structure field, measured in bytes.

Note
This variable attribute is a GNU compiler extension that the ARM compiler supports.
Example
/* Aligns on 16-byte boundary */
int x __attribute__((aligned (16)));
/* In this case, the alignment used is the maximum alignment for a scalar data type. For ARM, this is 8 bytes. */
short my_array __attribute__((aligned));

海迹天涯 发表于 2017-2-15 11:45:04

creep 发表于 2017-2-15 10:34
__ALIGN_BEGIN 和 __ALIGN_END 应该是 关于__align 的宏定义,说的都是字节对齐相关的定义:

...

英文太差怎么办:'(

creep 发表于 2017-2-15 11:47:46

海迹天涯 发表于 2017-2-15 11:45
英文太差怎么办

随便百度一下__align吧。。。:)

海迹天涯 发表于 2017-2-15 12:30:44

creep 发表于 2017-2-15 10:36
除了上面还有__attribute__((aligned)) 的用法

那么问题来了;这里是这样描述的__align(n) is useful when the normal alignment of the variable being declared is less than n.大概意思是说如果之前的边界对齐大小小于要声明的才需要使用,但是我追踪这里的代码      #define __ALIGN_BEGIN    __align(4)意思是以4字节对齐,但是这个结构体中全部都是1个字节的元素,请问怎么理解?

海迹天涯 发表于 2017-2-15 12:32:34

海迹天涯 发表于 2017-2-15 12:30
那么问题来了;这里是这样描述的__align(n) is useful when the normal alignment of the variable being ...

而且32位MCU默认就是4字节对齐吧,这里没必要再声明一次啊,也许我理解错误,望大神指正

creep 发表于 2017-2-15 13:50:21

海迹天涯 发表于 2017-2-15 12:32
而且32位MCU默认就是4字节对齐吧,这里没必要再声明一次啊,也许我理解错误,望大神指正 ...

这个4字节对齐是在某种模式下强制使用的,你看下是不是你的USB工作在HS或者启动了DMA.

/* In HS mode and when the DMA is used, all variables and data structures dealing
   with the DMA during the transaction process should be 4-bytes aligned */   
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
#if defined   (__GNUC__)      /* GNU Compiler */
    #define __ALIGN_END    __attribute__ ((aligned (4)))
    #define __ALIGN_BEGIN         
#else                           
    #define __ALIGN_END
    #if defined   (__CC_ARM)      /* ARM Compiler */
      #define __ALIGN_BEGIN    __align(4)
    #elif defined (__ICCARM__)    /* IAR Compiler */
      #define __ALIGN_BEGIN
    #elif defined(__TASKING__)/* TASKING Compiler */
      #define __ALIGN_BEGIN    __align(4)
    #endif /* __CC_ARM */
#endif /* __GNUC__ */
#else
#define __ALIGN_BEGIN
#define __ALIGN_END   
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */

队长shiwo 发表于 2017-2-16 08:50:26

不了解这个关键字
页: [1]
查看完整版本: C语言问题请教