例如: Sizeof (char*)返回4。还有int* long long*,我试过的所有方法。有什么例外吗?
当前回答
如果您正在为64位机器编译,那么它可能是8。
其他回答
如果您正在为64位机器编译,那么它可能是8。
据我所知,这是基于内存地址的大小。所以在一个32位地址方案的系统上,sizeof将返回4,因为那是4个字节。
指针大小为4字节的原因是因为您正在为32位体系结构编译。正如FryGuy所指出的,在64位架构上你将看到8。
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字节。
只是已经发布的列表中的另一个例外。在32位平台上,指针可以占用6字节,而不是4字节:
#include <stdio.h>
#include <stdlib.h>
int main() {
char far* ptr; // note that this is a far pointer
printf( "%d\n", sizeof( ptr));
return EXIT_SUCCESS;
}
如果你用Open Watcom编译这个程序并运行它,你会得到6,因为它支持的远指针由32位偏移量和16位段值组成