在计算数溢出时,想到了这个问题。
一般书本上都会写以16位计算机为例,或者直接默认为16位计算机,然后讲int占用2个字节。然后我们在自己的32位计算机上测试时往往会得到4个字节的结果。
于是给我们留了这样一个错觉:与计算机的位数有关。
其实不然。
As you can see, the typical data type sizes match the ILP32LL model, which is what most compilers adhere to on 32-bit platforms. The LP64 model is the de facto standard for compilers that generate code for 64-bit platforms.
ILP32为例,是指计算机所采用的数据模型。
事实上,int所占用的字节数与编译器和数据模型是有关的。
本人在VC6.0、devc++、wintc上进行了测试,得到了如下图所示的结果。
可以看到同一台电脑上WINTC的int是2字节,而VC和DEV都是4个字节。
参考资料:http://blog.youkuaiyun.com/cqupt_chen/article/details/8056023,本文第一张图引自该博文。