为什么的sizeof(int)的跨不同操作系统有什么不同? [英] Why does sizeof(int) vary across different operating systems?

查看:339
本文介绍了为什么的sizeof(int)的跨不同操作系统有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我wounder为什么为int的大小取决于OS的人使用,在C&放大器; C ++。它的确定,如果指针的大小而异,但为什么整数的大小。如果16​​位操作系统的sizeof(INT)= 2字节,对于32位的sizeof(int)的= 4字节。为什么会这样?

I wounder why size of int depends on which OS one is using ,in C & C++. It's ok if size of pointer varies, but why size of integer. If 16 bit OS sizeof(int) = 2 byte, for 32 bit sizeof(int) = 4 byte. Why so?

感谢。

推荐答案

根据C ++标准

1.7.1规定:

在C ++内存模型的基本存储单元是字节。一个
  字节是至少足以包含基本的任何成员
  执行字符集...

The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set ...

然后3.9.1.1状态:

then 3.9.1.1 states:

声明为字符(char)的对象要足够大,以存储
  实施的基本字符集的任何成员。

Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set.

因此​​,我们可以推断,字符实际上是一个字节。更重要的是3.9.1.2也说:

So we can infer that char is actually a byte. Most importantly 3.9.1.2 also says:

有网络连接已经签署整数类型:符号字符,短整型,诠释,
  长整型,和长长整型。在此列表中,每种类型提供在
  至少尽可能多的存储那些$ P $列表pceding它。平原整数
  有自然大小由执行的架构建议
  环境;设置在其它符号整数类型来满足
  特殊的需求。

There are five signed integer types: "signed char", "short int", "int", "long int", and "long 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 environment; the other signed integer types are provided to meet special needs.

所以,换句话说大小 INT 终止(一)保证至少有一个字节和(b)自然对齐到它是如此运行的OS /硬件最有可能的,这些天是64位或(对于许多旧系统)32位。

So in other words the size of int is (a) guaranteed to be at least a byte and (b) naturally aligned to the OS/hardware it's running on so most likely these days to be 64 bit or (for many older systems) 32 bit.

这篇关于为什么的sizeof(int)的跨不同操作系统有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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