在http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0065d/Cihejcda.html有一个选项和一个注释:
Enum container always int
Select this option to force all enumerations to be stored in integers. By default, the compiler uses the smallest data type that can hold all values in an enum.
Note
This option is not recommended for general use and is not required for ANSI-compatible source. If used incorrectly, this option can cause errors in the resulting image.
这种不正确使用的 C 语言示例是什么?
背景:
我正在用 C 开发一个嵌入式应用程序,删除这个编译器标志修复了一个错误(或只是另一个错误的症状)。错误是一个
sprintf(big_enought_char_array, "%4.1f", float_var)
函数调用没有正确格式化 float 。
这几乎可以通过对用作堆栈的内存使用正确的对齐方式来解决。 “几乎”修复了,因为数组的内容没问题,但小数点有错误的字符。删除上面提到的编译器选项解决了这个问题。所以问题是这个编译器选项究竟在做什么,什么是错误使用?
最佳答案
我假设您指的是这个选项:
Enum container always int
Select this option to force all enumerations to be stored in integers. By default, the compiler uses the smallest data type that can hold all values in an enum.
在 C 中,用于枚举类型的类型是实现定义的(但 enum
常量始终是 int
):它是一个整数类型,可以表示所有 enum
常量。
如果将枚举类型的类型设置为int
,我可以想到两个问题:
1) 如果您的程序依赖于 enum
是 int
这一事实(例如假设 sizeof
与 sizeof (int)
), 它在其他系统上不可移植
2) 如果您定义了一个值大于 INT_MAX
的常量(或者 int
无法表示),例如 UINT_MAX
,您的类型将无法在保证 C enum
代表它的地方代表它。
关于 sprintf
调用的具体问题,它似乎与此选项完全无关。您可能在调用之前调用了一些未定义的行为。
关于c - 错误使用 ARM C 编译器选项 "Enum container always int"的示例是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33365791/