本帖最后由 watershade 于 2019-7-22 17:37 编辑 # F8 M! x, Y5 s( E& ^; k) c 第一则:在keil枚举enum的size到底有多大? 如题,在结构体中难免要用到enum,而这时若需要估算结构体的size就需要知道enum类型的size。这个问题很早就遇见过,但是没有做好记录。 这次根据查到的官网资料做一个简单描述:) ~ `% ]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
Implementing 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. NoteCare 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~, 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的作用?
起初的确令人费解。但是如果你去掉之后编译就会显示一些warning,告知你有一些已经定义了的变量未使用。想必你之前一定遇到过类似状况。5 s; h% F; H8 T4 k, \ 5 g* F( m4 a9 i1 Y |
谢谢。其实蛮简单的。本来我翻译了一半,结果因为原文靠在代码格式里。一保存啥也没有了,就没再翻译一遍