Cya*_*yan 8 c 64-bit portability
是sizeof(size_t)==8说,该平台是64位的相同呢?相反,sizeof(size_t)==4相当于说平台是32位?
更重要的是,在所有情况下,此测试是否安全可靠,并牢记操作系统和编译器的可移植性?是否有一些奇怪的角落案例,包括size_t可能丢失的潜在情况?
我有点担心size_t可能只能保证C99环境.
Practically speaking, yes, this is safe and reliable. Platforms you are likely to target or ever target in the future are all byte-addressable, with 8-bit bytes and size_t equal to the machine word length. Most platforms provide guarantees that this will continue to be the case indefinitely (POSIX guarantees this, for example).
从理论上讲,不,这不安全可靠.诸如Cray-1,PDP-10和各种DSP系统之类的模糊系统将使您失望.但是,考虑一下:你有没有机会为Cray-1设计软件,这个软件在坐在你旁边的初级工程师出生之前已经过时了?
size_t 是一种能够表示任何对象大小的数据类型.
64位通常是指64位可用于寻址虚拟内存的事实.在C中,使用指针来寻址内存.因此,sizeof(void*)似乎更适合测试64位环境.
但是,C标准无法保证这一点.可能存在一些模糊的情况,其中没有安全可靠的方法来确定使用C存在的硬件架构.
Because sizeof returns the size as multiples of the size of a char, you might want to look at CHAR_BIT (defined in limits.h) to see how many bits there are in a char.