没有任何理由不使用固定宽度整数类型(如uint8_t有)? [英] Is there any reason not to use fixed width integer types (e.g. uint8_t)?

查看:212
本文介绍了没有任何理由不使用固定宽度整数类型(如uint8_t有)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你正在使用支持C99(甚至只是stdint.h)编译器,没有任何理由不使用固定宽度的整数类型,如uint8_t有?

Assuming you're using a compiler that supports C99 (or even just stdint.h), is there any reason not to use fixed width integer types such as uint8_t?

一个原因,我所知道的是,它使更多的意义与字符,而不是使用时务请使用字符 s ^ (U )中int8_t S,如这个问题。

One reason that I'm aware of is that it makes much more sense to use chars when dealing with characters instead of using (u)int8_ts, as mentioned in this question.

但如果你是在储存号码的计划,当你想使用一种类型,你不知道它有多大?即在什么情况下你会想存储在若干无符号短不知道如果是8,16,甚至是32位,而不是使用 uint16t

But if you are planning on storing a number, when would you want to use a type that you don't know how big it is? I.e. In what situation would you want to store a number in a unsigned short without knowing if it is 8, 16, or even 32 bits, instead of using a uint16t?

根据这一点,是它认为更好的做法是使用固定宽度的整数,或者使用普​​通的整数类型,只是从来没有假设任何事情,并使用的sizeof 等。无论您需要知道他们有多少字节使用?

Following on from this, is it considered better practice to use fixed width integers, or to use the normal integer types and just never assume anything and use sizeof whereever you need to know how many bytes they are using?

推荐答案

这其实很常见的存储号码,而无需知道是什么类型的确切大小。有很多数量的在我的节目,我可以合理地假设将不超过2十亿或者强制他们不这样做。但是,这并不意味着我需要一个精确的32位类型来存储它们,任何类型的可以数到至少2个十亿是罚款由我。

It's actually quite common to store a number without needing to know the exact size of the type. There are plenty of quantities in my programs that I can reasonably assume won't exceed 2 billion, or enforce that they don't. But that doesn't mean I need an exact 32 bit type to store them, any type that can count to at least 2 billion is fine by me.

如果你想写得非常便携code,你必须牢记,固定宽度类型均可选

If you're trying to write very portable code, you must bear in mind that the fixed-width types are all optional.

在一个C99实现,其中 CHAR_BIT 大于 8 没有中int8_t 。该标准禁止其存在,因为它必须有填充比特,而 intN_t 类型被定义为没有填充位(7.18.1.1/1)。 uint8_t有也因此被禁止,因为(感谢,ouah)的实现是不允许定义 uint8_t有没有中int8_t

On a C99 implementation where CHAR_BIT is greater than 8 there is no int8_t. The standard forbids it to exist because it would have to have padding bits, and intN_t types are defined to have no padding bits (7.18.1.1/1). uint8_t therefore also forbidden because (thanks, ouah) an implementation is not permitted to define uint8_t without int8_t.

所以,非常便携code,如果你需要能够容纳值高达127,那么你应该使用一个符号类型符号字符 INT int_least8_t int_fast8_t 编译器,使其:

So, in very portable code, if you need a signed type capable of holding values up to 127 then you should use one of signed char, int, int_least8_t or int_fast8_t according to whether you want to ask the compiler to make it:


  • 在C89的工作(符号字符 INT

  • 避免算术前pressions令人惊讶的整数促销( INT

  • 小( int_least8_t 符号字符

  • 快( int_fast8_t INT

  • work in C89 (signed char or int)
  • avoid surprising integer promotions in arithmetic expressions (int)
  • small (int_least8_t or signed char)
  • fast (int_fast8_t or int)

这同样适用于无符号类型多达255个,与 unsigned char型 unsigned int类型 uint_least8_t uint_fast8_t

The same goes for an unsigned type up to 255, with unsigned char, unsigned int, uint_least8_t and uint_fast8_t.

如果您需要模256算术非常便携code,那么你可以自己拿模数,掩码位,或者与位域玩游戏。

If you need modulo-256 arithmetic in very portable code, then you can either take the modulus yourself, mask bits, or play games with bitfields.

在实践中,大多数人从来没有需要写code便携。目前 CHAR_BIT> 8 只出现在专用的硬件,你的通用code不会得到它使用。当然,这可能会在未来发生变化,但如果这样做我怀疑有这么多code,使有关POSIX和/或Windows(两者都保证 CHAR_BIT == 8 ),即处理您code的非便携性将是一个很大的努力,端口code到新平台的一个小部分。任何这样的实现可能将不得不担心如何连接到互联网(其中涉及在字节),很久以前就担心如何让你的code运行起来: - )

In practice, most people never need to write code that portable. At the moment CHAR_BIT > 8 only comes up on special-purpose hardware, and your general-purpose code won't get used on it. Of course that could change in future, but if it does I suspect that there is so much code that makes assumptions about Posix and/or Windows (both of which guarantee CHAR_BIT == 8), that dealing with your code's non-portability will be one small part of a big effort to port code to that new platform. Any such implementation is probably going to have to worry about how to connect to the internet (which deals in octets), long before it worries how to get your code up and running :-)

如果你假设 CHAR_BIT == 8 反正那么我不认为有什么特别的原因,以避免(U)中int8_t 比,如果你想要的code在C89其他工作。即使在C89这并不难找到或编写一个版本 stdint.h 的特定实现。但是,如果你可以的轻松的写code到只需要该类型可以持有 255 ,而不是要求它的即可'T 的持有 256 ,那你还不如避免对依赖 CHAR_BIT == 8

If you're assuming that CHAR_BIT == 8 anyway then I don't think there's any particular reason to avoid (u)int8_t other than if you want the code to work in C89. Even in C89 it's not that difficult to find or write a version of stdint.h for a particular implementation. But if you can easily write your code to only require that the type can hold 255, rather than requiring that it can't hold 256, then you might as well avoid the dependency on CHAR_BIT == 8.

这篇关于没有任何理由不使用固定宽度整数类型(如uint8_t有)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆