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

语法问题l两则:在keil枚举enum的size到底有多大?|| (void)a作用

[复制链接]
watershade 发布时间:2019-7-22 16:13
本帖最后由 watershade 于 2019-7-22 17:37 编辑
+ T+ w- Z( R2 p) S% R! I# F8 M! x, Y5 s( E& ^; k) c
第一则:在keil枚举enum的size到底有多大?
& c' T. X; s$ X, K4 i( v, p如题,在结构体中难免要用到enum,而这时若需要估算结构体的size就需要知道enum类型的size。这个问题很早就遇见过,但是没有做好记录。
% c) _2 @* e- n" J: X% g* x这次根据查到的官网资料做一个简单描述:) ~  `% ]5 }' o  J' Z) h
原文如下:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~9 R# l( |; h8 [) k' w6 e; g& d
Enumerations- b9 U) @  N/ [+ f, r" m# e3 Q' ~' h
An object of type enum is implemented in the smallest integral type that contains the range of the enum.6 I, t1 y$ j( H3 E
In C mode, and in C++ mode without --enum_is_int, if an enum contains only positive enumerator values, the storage type of the enum is the first unsigned type from the following list, according to the range of the enumerators in the enum. In other modes, and in cases where an enum contains any negative enumerator values, the storage type of the enum is the first of the following, according to the range of the enumerators in the enum:' z2 r; X; m/ U) M
  • unsigned char if not using --enum_is_int
  • signed char if not using --enum_is_int
  • unsigned short if not using --enum_is_int
  • signed short if not using --enum_is_int
  • signed int
  • unsigned int except C with --strict
  • signed long long except C with --strict
  • unsigned long long except C with --strict.
    ( J: X  y$ B$ d' p2 A/ }3 K
Note
  • In RVCT 4.0, the storage type of the enum being the first unsigned type from the list was only applicable in GNU (--gnu) mode.
  • In ARM® Compiler 4.1 and later, the storage type of the enum being the first unsigned type from the list applies irrespective of mode.0 O: `3 @! k" c0 z

9 f' }8 v# r* ?4 j# d$ q$ BImplementing enum in this way can reduce data size. The command-line option --enum_is_int forces the underlying type of enum to at least as wide as int.6 i, v$ M( R5 E
See the description of C language mappings in the Procedure Call Standard for the ARM® Architecturespecification for more information.
  x0 _- E! R+ X( RNoteCare must be taken when mixing translation units that have been compiled with and without the --enum_is_intoption, and that share interfaces or data structures.+ P4 ~8 p/ X2 W
In strict C, enumerator values must be representable as ints. That is, they must be in the range -2147483648 to +2147483647, inclusive. A warning is issued for out-of-range enumerator values:  f  \; `  @& K, c1 q
#66: enumeration value is out of "int" rangeSuch values are treated the same way as in C++, that is, they are treated as unsigned int, long long, or unsigned long long.- L) s7 C# _9 l; [7 x2 x2 Q( B
To ensure that out-of-range Warnings are reported, use the following command to change them into Errors:1 Q, G0 \: t& u% L4 J3 C# P
armcc --diag_error=66 ...
5 f9 h" N& y$ W6 n

4 d8 q# a7 P9 a# t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~, t5 j* g( {( ~, H
/ L" b( L) W; @1 [5 k* v5 F
注意这个定义和你在开发win/unix程序是不一样的。在那种状况下一般默认是4个byte的数据。这点你需要自己做个测试程序测试一下。, h0 J$ r% l$ _' U7 _! m5 V
" F. T$ D  W( U  q( q/ a! D
第二则:在函数中使用(void)a的作用?
  1. #define UNUSED_VARIABLE(X)  ((void)(X))
    : T6 X" W# j! z# R! ?5 S9 Y
  2. #define UNUSED_PARAMETER(X) UNUSED_VARIABLE(X)
    1 j2 G( {# b) s& C
  3. static void battery_level_meas_timeout_handler(void * p_context)4 T6 b7 x# r3 F9 v9 ]
  4. {
    ) Y& {+ |6 k, @
  5.     UNUSED_PARAMETER(p_context);* Y  `6 ?9 ]) V. {3 O  s# @
  6.     battery_level_update();+ R$ y7 a% w" g; ~. ~( I0 C+ h
  7. }
复制代码
这是从nrf52的examples摘录的一段代码。这里的UNUSED_PARAMETER和UNUSED_VARIABLE是专门定义的两个宏。你可能会问这么大费周章的对一个未使用的形参做个(void)类型的强制转换有啥用?; s9 Z. n( F9 r' S) Q7 N
起初的确令人费解。但是如果你去掉之后编译就会显示一些warning,告知你有一些已经定义了的变量未使用。想必你之前一定遇到过类似状况。5 s; h% F; H8 T4 k, \
5 g* F( m4 a9 i1 Y

" E/ w3 s; F$ f7 c# S
8 Z0 O5 G/ T, ]4 K$ T

& I% f) Y( C, T6 c; |" p4 Y
* z0 i" Q. ~0 a  X4 b
( p; E8 x& j3 a! S- g, h+ f$ l9 V& q' ?$ Z
收藏 评论10 发布时间:2019-7-22 16:13

举报

10个回答
ksyxt0700379 回答时间:2019-7-22 17:01:39
不懂帮顶
watershade 回答时间:2019-7-22 17:35:59
ksyxt0700379 发表于 2019-7-22 17:01+ u& O" u9 a# c# \( r; H
不懂帮顶

0 T5 _/ K2 w) m" D谢谢。其实蛮简单的。本来我翻译了一半,结果因为原文靠在代码格式里。一保存啥也没有了,就没再翻译一遍
子曰好人 回答时间:2019-7-23 09:13:26
感谢分享
单片机爱好者 回答时间:2019-8-14 10:44:27
然后呢?   我关心结果
zzb666666 回答时间:2019-8-14 11:19:53
路过.
generalcircuits 回答时间:2019-8-14 15:36:04
没看明白,楼主的意思
andey 回答时间:2019-8-14 17:45:19
提示: 作者被禁止或删除 内容自动屏蔽
ksyxt0700379 回答时间:2019-8-15 11:50:55
好吧,还是没懂
ksyxt0700379 回答时间:2019-8-15 11:51:23
具体是为什么要这样做呢  s, z( r3 a+ v4 O
哀歌与世无争 回答时间:2019-8-15 13:35:03
太长不看,咋不直接给结论

所属标签

相似分享

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版