Item 21: Avoid Problems Related to the Size and Organization of Types
有件事情好像我一直都搞错了, C/C++最基本的数据类型的大小. 标准并没有规定char/short/int/long的数据大小, 而只规定了它们之间的关系:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
VC的话, x86-32/x86-64的机器大小都是一样的, 分别为1, 2, 4, 4, 8. 注意long long是c99中加入的. 如果gcc的话, 我没有研究过. c99中加入了一系列的macro: (u)int8_t, (u)int16_t, (u)int32_t, (u)int64_t似乎能解决一些问题, 但是vs2005目前还不支持.
第二个问题: 一个char到底包含多少个byte? 多少个bit?
也许你这次会谨慎的说, x86的话, 分别是1和8. 嗯.. 对于byte来说, 标准说就是一个byte, 但是一个byte是多少个bit是没有说明的, 也就是说, 8,16,32,64,128都是可以的=v=.
第三个问题, word是个什么东西?
最初看到这个还是在ICS课的教材. 今天又看到了说for循环为什么一般都用int来做, 理由是cpu做的事情最少最高效(x86是32为寻址的). 而word一般是指natural的数据大小(natural不知道怎么翻译), x86就是32bit. 可是VC下的WORD显然被定义成了16bit的short啊, 怎么回事? 唉.. 我直接抄wikipedia吧:
Sometimes the size of a word is defined to be a particular value for compatibility with earlier computers. The most common microprocessors used in personal computers (for instance, the Intel Pentiums and AMD Athlons) are an example of this. Their IA-32 architecture is an extension of the original Intel 8086 design which had a word size of 16 bits. The IA-32 processors still support 8086 (x86) programs, so the meaning of "word" in the IA-32 context was kept the same, and is still said to be 16 bits, despite the fact that they at times (especially when the default operand size is 32-bit) operate largely like a machine with a 32 bit word size. Similarly in the newer x86-64 architecture, a "word" is still 16 bits, although 64-bit ("quadruple word") operands may be more common.
参考:
*) How should I decide which integer type to use?
*) Why aren't the sizes of the standard types precisely defined?
*) Since C doesn't define sizes exactly, I've been using typedefs like int16 and int32. I can then define these typedefs to be int, short, long, etc. depending on what machine I'm using. That should solve everything, right?
*) C++ Integer Data Types
*) Integer (computer science)
*) Word (computing)



