3字节int和5字节长? [英] 3-byte int and 5-byte long?

查看:315
本文介绍了3字节int和5字节长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道每个C和C ++标准允许的sizeof 数值类型的的是2的幂?

Does each of C and C++ standards allow sizeof of numeric types not to be a power of two?

下面的约束是已知的:


  • 16< = CHAR_BIT * sizeof的(INT)< = CHAR_BIT * sizeof的(长)

  • 32< = CHAR_BIT * sizeof的(长)< = CHAR_BIT * sizeof的(很长很长)

  • 和其他十几个,其中一个典型的8位字节架构意味着 2'= sizeof的(INT)及和放大器; 4'= sizeof的(长)

  • 16 <= CHAR_BIT * sizeof(int) <= CHAR_BIT * sizeof(long)
  • 32 <= CHAR_BIT * sizeof(long) <= CHAR_BIT * sizeof(long long)
  • and a dozen of others, which on a typical 8-bit byte architecture means 2 <= sizeof(int) && 4 <= sizeof(long)

这是否意味着的sizeof(int)的== 3安培;&安培;的sizeof(长)== 5 是一个有效的行为?

Does that mean that sizeof(int) == 3 && sizeof(long) == 5 is a valid behaviour?

如果是 - ?是否有任何已知的编译器/架构以类似的方式表现

If yes - is there any known compiler/architecture behaving in a similar way?

推荐答案

我觉得3.9.1 / 2(C ++ 98)资金这件事很好地(紧跟着的是无符号类型相似的信息):

I think 3.9.1/2 (C++98) sums this up nicely (immediately followed by analogous information for the unsigned types):

有四种符号整型:符号字符,短整型,
  INT和长整型。在这份名单中,每种至少提供
  很多存储那些$ P $列表pceding它。平原整数有
  由执行的架构建议自然大小
  environment39);设置在其它符号整数类型来满足
  特殊的需求。

There are four signed integer types: "signed char", "short int", "int", and "long int." In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment39) ; the other signed integer types are provided to meet special needs.

基本上所有我们知道的是,的sizeof(char)的== 1 ,每个大型是至少是大,具有 INT 是一个体系结构(其中据我可以告诉自然要由编译器作者),自然的大小。我们不知道像 CHAR_BIT任何*的sizeof(int)的&LT; = 32 等。也请记住, CHAR_BIT 不必须是8任

Basically all we know is that sizeof(char) == 1 and that each "larger" type is at least that large, with int being a "natural" size for an architecture (where as far as I can tell "natural" is up to the compiler writer). We don't know anything like CHAR_BIT * sizeof(int) <= 32 etc. Also keep in mind that CHAR_BIT doesn't have to be 8 either.

它似乎相当肯定地说有三个字节 INT 和五个字节将被允许对硬件哪里那些尺寸在本机使用。不过,我不知道有任何这样的硬件/架构。

It seems fairly safe to say that three byte int and five byte long would be allowed for hardware where those sizes were natively used. I am however not aware of any such hardware/architectures.

编辑:由于在@Nigel哈珀评论指出,我们知道 INT 必须至少为16位和至少32位,以满足范围要求。否则,我们没有比上面所看到其他任何特定尺寸的限制。

As pointed out in @Nigel Harper comment we do know that int has to be at least 16 bits and long at least 32 bits to satisfy range requirements. Otherwise we don't have any specific size restrictions other than as seen above.

这篇关于3字节int和5字节长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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