[英]Why do I see 64-bit pointers in C++ on my 32-bit Mac OS X system?
So I've read a lot of related posts on SO and elsewhere such as:所以我已经阅读了很多关于 SO 和其他地方的相关帖子,例如:
Is the sizeof(some pointer) always equal to four? sizeof(some pointer) 总是等于四吗?
It makes total sense to me that on a 32-bit system I would expect 4-byte pointers and on a 64-bit system I would expect 8-byte pointers.对我来说,在 32 位系统上我希望使用 4 字节指针,而在 64 位系统上我希望使用 8 字节指针,这是完全合理的。 So I'm running this code:
所以我正在运行这个代码:
int main()
{
cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(void*) = " << sizeof(void*) << endl;
}
And this is the corresponding output:这是相应的输出:
sizeof(int) = 4
sizeof(void*) = 8
I'm running in 32-bit mode on Mac OS X 10.6.1.我在 Mac OS X 10.6.1 上以 32 位模式运行。 Here's the output of "uname -a":
这是“uname -a”的输出:
Darwin brents-macbook.local 10.0.0 Darwin Kernel Version 10.0.0: Fri Jul 31 22:47:34 PDT 2009; root:xnu-1456.1.25~1/RELEASE_I386 i386 i386
Here's the version of g++ I'm running (default that came with the system):这是我正在运行的 g++ 版本(系统自带的默认值):
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646)
So I realize that the sizes of pointers are not guaranteed from system to system and they're completely dependent on compiler and architecture, but does this result strike anyone else as illogical?所以我意识到指针的大小不能保证从系统到系统,它们完全依赖于编译器和架构,但是这个结果是否让其他人觉得不合逻辑? Is this just an idiosyncrasy of Mac OS X 10.6 or my setup?
这只是 Mac OS X 10.6 或我的设置的特性吗? Or is there a good reason I'm seeing pointers of this size?
或者我看到这种大小的指针是否有充分的理由?
================================================== ==================================================
Extra details for anyone who wants them...任何想要它们的人的额外细节......
I was originally compiling with this command line:我最初是用这个命令行编译的:
g++ -Wall -o TestClass1 TestClass1.cpp
And it generated this output:它生成了这个输出:
sizeof(int) = 4
sizeof(void*) = 8
After the suggestion below, I changed my command line to this:在下面的建议之后,我将命令行更改为:
g++ -Wall -o TestClass1 -arch i386 TestClass1.cpp
And the output changes to this:输出更改为:
sizeof(int) = 4
sizeof(void*) = 4
You're running a 32-bit kernel, but you're compiling the code into a 64-bit executable.您正在运行 32 位内核,但您正在将代码编译为 64 位可执行文件。 Both 32- and 64-bit code can run in OS X, regardless of which kernel is in use.
无论使用的是哪个内核,32 位和 64 位代码都可以在 OS X 中运行。
If you want to compile the code into a 32-bit executable, pass the -arch i386
flag to gcc.如果要将代码编译为 32 位可执行文件,请将
-arch i386
标志传递给 gcc。 The corresponding flag for 64-bit is -arch x86_64
, but it is the default on Snow Leopard. 64 位的相应标志是
-arch x86_64
,但它是 Snow Leopard 的默认设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.