你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

C语言问题请教

[复制链接]
海迹天涯 提问时间:2017-2-15 09:56 /
今天看到一个新的关键字不能理解在这里的作用,图中红色方框内的代码,有没有大神能分析并解释下,感激不尽!
R`$PRR~6BZ3A5)]YEWZ}F_G.png
收藏 1 评论9 发布时间:2017-2-15 09:56

举报

9个回答
MrJiu 回答时间:2017-2-15 10:21:17
你先右键跟踪一下,有可能是一个define定义的其他东西....或者特定编译器自己支持的关键字等等....总之就是右键先跟踪一下,这个东西是代表啥!!!

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

creep 回答时间:2017-2-15 10:34:31
__ALIGN_BEGIN 和 __ALIGN_END 应该是 关于__align 的宏定义,说的都是字节对齐相关的定义:
  1. 9.2 __align
  2. The __align keyword instructs the compiler to align a variable on an n-byte boundary.

  3. __align is a storage class modifier. It does not affect the type of the function.
  4. Syntax
  5. __align(n)
  6. Where:
  7. n
  8. is the alignment boundary.
  9. For local variables, n can take the values 1, 2, 4, or 8.
  10. For global variables, n can take any value up to 0x80000000 in powers of 2.
  11. Usage
  12. __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.
  13. __align can be used in conjunction with extern and static.
  14. Restrictions
  15. Because __align is a storage class modifier, it cannot be used on:
  16. Types, including typedefs and structure definitions.
  17. Function parameters.
  18. 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.
  19. Example
  20. __align(8) char buffer[128];  // buffer starts on eight-byte boundary
  21. void foo(void)
  22. {
  23.     ...
  24.     __align(16) int i; // this alignment value is not permitted for
  25.                        // a local variable
  26.     ...
  27. }
  28. __align(16) int i; // permitted as a global variable.
复制代码

评分

参与人数 1ST金币 +3 收起 理由
Tcreat + 3 赞一个!

查看全部评分

creep 回答时间:2017-2-15 10:36:10
除了上面还有__attribute__((aligned)) 的用法

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

  3. Note
  4. This variable attribute is a GNU compiler extension that the ARM compiler supports.
  5. Example
  6. /* Aligns on 16-byte boundary */
  7. int x __attribute__((aligned (16)));
  8. /* In this case, the alignment used is the maximum alignment for a scalar data type. For ARM, this is 8 bytes. */
  9. short my_array[3] __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

随便百度一下__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.

  1. /* In HS mode and when the DMA is used, all variables and data structures dealing
  2.    with the DMA during the transaction process should be 4-bytes aligned */   
  3. #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
  4.   #if defined   (__GNUC__)        /* GNU Compiler */
  5.     #define __ALIGN_END    __attribute__ ((aligned (4)))
  6.     #define __ALIGN_BEGIN         
  7.   #else                           
  8.     #define __ALIGN_END
  9.     #if defined   (__CC_ARM)      /* ARM Compiler */
  10.       #define __ALIGN_BEGIN    __align(4)  
  11.     #elif defined (__ICCARM__)    /* IAR Compiler */
  12.       #define __ALIGN_BEGIN
  13.     #elif defined  (__TASKING__)  /* TASKING Compiler */
  14.       #define __ALIGN_BEGIN    __align(4)
  15.     #endif /* __CC_ARM */  
  16.   #endif /* __GNUC__ */
  17. #else
  18.   #define __ALIGN_BEGIN
  19.   #define __ALIGN_END   
  20. #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
复制代码
队长shiwo 回答时间:2017-2-16 08:50:26
不了解这个关键字

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版