c - 错误使用 ARM C 编译器选项 "Enum container always int"的示例是什么?

标签 c arm compiler-options

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) 如果您的程序依赖于 enumint 这一事实(例如假设 sizeofsizeof (int)), 它在其他系统上不可移植

2) 如果您定义了一个值大于 INT_MAX 的常量(或者 int 无法表示),例如 UINT_MAX ,您的类型将无法在保证 C enum 代表它的地方代表它。

关于 sprintf 调用的具体问题,它似乎与此选项完全无关。您可能在调用之前调用了一些未定义的行为。

关于c - 错误使用 ARM C 编译器选项 "Enum container always int"的示例是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33365791/

相关文章:

c - 检测字符流结束

c - 为什么 C 函数 strlen() 返回错误的 char 长度?

c - 如何在带有 Verix OS 的 Verifone 设备上使用 ARM 应用程序上的 read() 函数或类似函数

arm - 有没有办法将数据从应用程序直接发送到 JTAG 端口? (皮质-M3)

c - 使用 Oracle 10g 64 位构建 PROC 应用程序的问题

autotools - 如何将编译器标志从 Autoconf 传递到 Automake?

c++ - 是否有可能每次都在随机地址处加载FORTRAN DLL?

c - 在 libcurl 中发送表单

c++ - 为什么我在 clrscr(); 中遇到错误它说未定义?

c - Cortex-M7 的 DS-5 配置