tags:

views:

222

answers:

1

So I've read a lot of related posts on SO and elsewhere such as:

http://stackoverflow.com/questions/399003/is-the-sizeofsome-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 brent-nashs-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
+6  A: 

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.

Stephen Canon
Makes total sense. Didn't realize Snow Leopard was defaulting to 64-bit compilation. Thanks for the quick feedback! Maybe this is my cue to stop writing so much Java... ;)
Brent Nash
Also, you can use lipo to get info about the binary, or strip unwanted versions from fat binaries.
jeffamaphone
Happy to help. Note that OS X and Linux use the "LP64" model for 64-bit code, meaning that long is 64-bits wide (on 64-bit Windows systems, long is 32 bits) -- this often trips up people coming from a Windows background.
Stephen Canon