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:
Is the sizeof(some pointer) always equal to four?
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. 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. Here's the output of "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):
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? Or is there a good reason I'm seeing pointers of this size?
==================================================
Post-Answer Addition
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. Both 32- and 64-bit code can run in OS X, regardless of which kernel is in use.
If you want to compile the code into a 32-bit executable, pass the -arch i386
flag to gcc. The corresponding flag for 64-bit is -arch x86_64
, but it is the default on Snow Leopard.
精彩评论