例如: Sizeof (char*)返回4。还有int* long long*,我试过的所有方法。有什么例外吗?


当前回答

你得到的保证是sizeof(char) == 1。没有其他保证,包括没有保证sizeof(int *) == sizeof(double *)。

在实践中,指针的大小在16位系统上为2(如果您能找到一个),在32位系统上为4,在64位系统上为8,但是依赖于给定的大小并不能获得任何东西。

其他回答

如果您正在为64位机器编译,那么它可能是8。

一般来说,当你在不同的平台上编译时,sizeof(几乎任何东西)都会改变。在32位平台上,指针的大小总是相同的。在其他平台上(64位是最明显的例子),这种情况可能会发生变化。

The size of the pointer basically depends on the architecture of the system in which it is implemented. For example the size of a pointer in 32 bit is 4 bytes (32 bit ) and 8 bytes(64 bit ) in a 64 bit machines. The bit types in a machine are nothing but memory address, that it can have. 32 bit machines can have 2^32 address space and 64 bit machines can have upto 2^64 address spaces. So a pointer (variable which points to a memory location) should be able to point to any of the memory address (2^32 for 32 bit and 2^64 for 64 bit) that a machines holds.

由于这个原因,我们看到指针的大小在32位机器中是4字节,在64位机器中是8字节。

你得到的保证是sizeof(char) == 1。没有其他保证,包括没有保证sizeof(int *) == sizeof(double *)。

在实践中,指针的大小在16位系统上为2(如果您能找到一个),在32位系统上为4,在64位系统上为8,但是依赖于给定的大小并不能获得任何东西。

不,指针的大小可能因架构而异。有很多例外。