什么时候使用不同的整数类型? [英] When to use different integer types?

查看:57
本文介绍了什么时候使用不同的整数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编程语言(例如c,c ++和java)通常具有几种用于整数算术的类型:

Programming languages (e.g. c, c++, and java) usually have several types for integer arithmetic:

  • signedunsigned类型
  • 不同大小的类型:shortintlonglong long
  • 保证和不保证(即取决于实现)大小的类型:
    例如int32_t vs int(而且我知道int32_t不是语言的一部分)
  • signed and unsigned types
  • types of different size: short, int, long, long long
  • types of guaranteed and non guaranteed (i.e.implementation dependent) size:
    e.g. int32_t vs int (and I know that int32_t is not part of the language)

您如何总结何时应使用它们中的每一个?

How would you summarize when one should use each of them?

推荐答案

在几乎所有语言中,默认整数类型(int)均享有等值于先"的优惠待遇.因此,如果没有理由偏爱其他类型,我们可以将其用作默认值.

The default integral type (int) gets a "first among equals" preferential treatment in pretty much all languages. So we can use that as a default, if no reasons to prefer another type exist.

这种原因可能是:

  • 如果知道需要更大的范围,请使用较大的类型;如果要节省内存并且不介意较小的范围,请使用较小的类型.
  • 如果您打算使用位移运算符(<<>>).
  • 如果该语言不能保证某种类型的最小(甚至是固定的)大小(例如C/C ++与C#/Java),并且您关心它的属性,则应该采用某种机制来生成具有保证大小的类型的机制(例如int32_t)-如果您的程序具有可移植性,并且希望使用其他编译器进行编译,那么这将变得更加重要.
  • Using a bigger type if you know you need the additional range, or a smaller type if you want to conserve memory and don't mind the smaller range.
  • Using an unsigned type to make sure that you don't get any "extra" 1s in your integer representation if you intend to use bit shifting operators (<< and >>).
  • If the language does not guarantee a minimum (or even fixed) size for a type (e.g. C/C++ vs C#/Java), and you care about its properties, you should prefer some mechanism of generating a type with guaranteed size (e.g. int32_t) -- if your program is meant to be portable and expected to be compiled with different compilers, this becomes more important.

更新(扩展保证尺寸类型)

Update (expanding on guaranteed size types)

我个人的观点是,没有保证固定大小的类型比今天值得拥有的麻烦更多.我不会介绍产生它们的历史原因(简称:源代码可移植性),但是现实是,在2011年,很少有人(如果有的话)能从中受益.

My personal opinion is that types with no guaranteed fixed size are more trouble than worth today. I won't go into the historical reasons that gave birth to them (briefly: source code portability), but the reality is that in 2011 very few people, if any, stand to benefit from them.

另一方面,使用此类类型时,很多事情可能会出错:

On the other hand, there are lots of things that can go wrong when using such types:

  • 该类型没有足够的范围
  • 您访问变量的基础内存(可能是对其进行序列化),但是由于处理器的字节序和类型的不固定大小,最终导致引入错误

由于这些原因(可能还有其他原因),从理论上讲,使用此类类型是一个主要的难题.此外,除非需要极端可移植性,否则您根本无法从中受益.实际上,像int32_t这样的typedef的全部目的是完全消除对松散大小类型的使用.

For these reasons (and there are probably others too), using such types is in theory a major pain. Additionally, unless extreme portability is a requirement, you don't stand to benefit at all to compensate. And indeed, the whole purpose of typedefs like int32_t is to eliminate usage of loosely sized types entirely.

实际上,如果您知道程序不会移植到另一个编译器或体系结构,则可以忽略类型没有固定大小的事实,并将它们视为编译器的已知大小用于它们.

As a practical matter, if you know that your program is not going to be ported to another compiler or architecture, you can ignore the fact that the types have no fixed size and treat them as if they are the known size your compiler uses for them.

这篇关于什么时候使用不同的整数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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