[英]ARM cortex-M3 uint_fast32_t vs uint32_t
我正在为STM32Fx cortex-M3系列处理器开发程序。 在stdint.h中定义了以下内容:
typedef unsigned int uint_fast32_t;
typedef uint32_t uint_least32_t;
typedef unsigned long uint32_t;
据我了解。
[u]int_fast[n]_t will give you the fastest data type of at least n bits.
[u]int_least[n]_t will give you the smallest data type of at least n bits.
[u]int[n]_t will give you the data type of exactly n bits.
据我所知,sizeof(unsigned int)<= sizeof(unsigned long)和UINT_MAX <= ULONG_MAX-总是如此。
因此,我希望uint_fast32_t是一种数据类型,其大小等于或大于uint32_t的大小。
在皮质M3的情况下,sizeof(unsigned int)== sizeof(unsigned long)==4。因此,上述定义在大小方面是“正确的”。
但是为什么不以与基础数据类型的名称和逻辑大小一致的方式定义它们,即
typedef unsigned long uint_fast32_t;
typedef unsigned int uint_least32_t;
typedef uint_fast32_t uint32_t;
有人可以澄清一下基础类型的选择吗?
假设'long'和'int'的大小相同,为什么不对所有三个定义使用相同的数据类型?
typedef unsigned int uint_fast32_t;
typedef unsigned int uint_least32_t;
typedef unsigned int uint32_t;
情况是,只能保证
sizeof(long) >= sizeof(int)
并且不能保证它实际上不再存在。 在许多系统上,int通常与long一样长。
看看我对另一个问题的回答 。
基本上,使用哪种类型都没有关系。 假定int
和long
具有相同的大小,并且具有相同的表示形式和其他特征,则实现者可以为int32_t
, int_fast32_t
和int_least32_t
选择任何一种类型,并为相应的无符号版本选择相同的类型。
(有可能认为特定的选择可能会受到影响,因为对于使用int
和long
不同大小的实现,需要使用相同的标头,但是我看不到您引用的特定定义如何实现这一点。)
只要类型是正确的大小并且满足标准所强加的所有其他要求,并且只要您不编写依赖于例如int32_t
与int
或long
兼容的代码,就可以,没关系。
做出的特定选择可能是实施者的任意想法-这是完全可以接受的。 又或者,该头文件是由两个或两个以上的开发人员修改的,他们对哪种类型最好是有不同的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.