硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3434|回复: 10
收起左侧

[MDK] 分享一种结构体内成员指定对齐的语法

  [复制链接]

210

主题

1045

回帖

1685

积分

至尊会员

More we do, more we can do.

积分
1685
发表于 2021-6-19 15:57:58 | 显示全部楼层 |阅读模式
最近数据存EEPROM带ECC,考虑到使用寿命,按规格书要求4字节存1个参数,搞出了这个写法。

  1. #include <stdio.h>
  2. #include <stdint.h>

  3. typedef struct
  4. {
  5.     uint8_t __attribute__((aligned (4))) a;
  6.     uint8_t __attribute__((aligned (4))) b;
  7.     uint8_t __attribute__((aligned (4))) c;
  8. }Aligned_Struct;

  9. int main()
  10. {
  11.     Aligned_Struct demo;
  12.     printf("0x%X 0x%X 0x%X", (uint32_t)&demo.a, (uint32_t)&demo.b, (uint32_t)&demo.c);
  13.     return 0;
  14. }
复制代码


规格书写寿命脚注:
The Write cycle endurance is defined for groups of four data bytes located at addresses [4*N, 4*N+1, 4*N+2, 4*N+3] where N is an integer.
回复

使用道具 举报

3

主题

337

回帖

346

积分

高级会员

积分
346
发表于 2021-6-19 17:27:03 | 显示全部楼层
这句英文并不是说要你4字节只能存一个参数。而是说写寿命的最小单位是一个group,每个group由4字节组成。
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106978
QQ
发表于 2021-6-20 11:08:44 | 显示全部楼层
谢谢分享。

回复

使用道具 举报

73

主题

1198

回帖

1417

积分

至尊会员

积分
1417
发表于 2021-6-20 12:44:54 | 显示全部楼层
glory 发表于 2021-6-19 17:27
这句英文并不是说要你4字节只能存一个参数。而是说写寿命的最小单位是一个group,每个group由4字节组成。

正确~~~
回复

使用道具 举报

210

主题

1045

回帖

1685

积分

至尊会员

More we do, more we can do.

积分
1685
 楼主| 发表于 2021-6-21 12:23:28 | 显示全部楼层
glory 发表于 2021-6-19 17:27
这句英文并不是说要你4字节只能存一个参数。而是说写寿命的最小单位是一个group,每个group由4字节组成。

我这个延长寿命的做法有问题吗,应该怎么改进?
回复

使用道具 举报

4

主题

129

回帖

146

积分

初级会员

积分
146
发表于 2021-6-21 14:33:37 | 显示全部楼层
还有一种写法

  1. #pragma pack(4)
  2. typedef struct
  3. {
  4.     uint8_t a;
  5.     uint8_t b;
  6.     uint8_t c;
  7. }Aligned_Struct;
  8. #pragma pack()
复制代码
回复

使用道具 举报

335

主题

2037

回帖

3047

积分

版主

Rank: 7Rank: 7Rank: 7

积分
3047
发表于 2021-7-6 11:31:36 | 显示全部楼层

这些奇奇怪怪的语法,在哪里可以查到帮助文档?
回复

使用道具 举报

14

主题

62

回帖

104

积分

初级会员

积分
104
发表于 2021-7-6 15:31:54 | 显示全部楼层
  1. typedef struct
  2. {
  3.     uint32_t  a;
  4.     uint32_t  b;
  5.     uint32_t  c;
  6. }Aligned_Struct;
复制代码


跟这个有啥区别?
回复

使用道具 举报

77

主题

689

回帖

920

积分

金牌会员

积分
920
发表于 2021-7-6 15:57:34 | 显示全部楼层
这样兼容性太差了,直接使用uin32_t存每个数据就是四字节对其了呀
回复

使用道具 举报

210

主题

1045

回帖

1685

积分

至尊会员

More we do, more we can do.

积分
1685
 楼主| 发表于 2021-7-6 23:46:29 | 显示全部楼层

你的写法跟楼主位的写法效果不一样,参见GCC手册:
6.62.11 Structure-Layout Pragmas
For compatibility with Microsoft Windows compilers, GCC supports a set of #pragma directives that change the maximum alignment of members of structures (other than zero-width
bit-fields), unions, and classes subsequently defined. The n value below always is required
to be a small power of two and specifies the new alignment in bytes.
回复

使用道具 举报

0

主题

57

回帖

57

积分

初级会员

积分
57
发表于 2021-7-18 14:58:57 | 显示全部楼层
各位大仙是用的什么芯片,32位的编译器默认的结构体就是4字节对齐啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-13 04:28 , Processed in 0.223085 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表