简体   繁体   English

为什么我在 32 位 Mac OS X 系统上看到 C++ 中的 64 位指针?

[英]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?或者我看到这种大小的指针是否有充分的理由?

================================================== ==================================================

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.您正在运行 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.

相关问题 为什么在 C++ 中编译 64 位时会出现 32 位溢出? - Why do I get a 32-bit overflow when compiling in 64-bit in C++? 如何将 c++ 程序编译为 32 位可执行文件而不是 64 位 - How do I compile a c++ program into a 32-bit executable instead of 64-bit 如何将C ++项目从32位迁移到64位以支持MAC OS 10.14(Mojave)? - How to migrate C++ project from 32-Bit to 64-Bit to support MAC OS 10.14(Mojave)? 从c / c ++应用程序确定32位操作系统还是64位操作系统 - determine 32-bit OS or 64-bit OS from c/c++ application C ++,用于检查软件是否在32位或64位系统上运行的函数 - C++, function that checks if software runs on 32-bit or 64-bit system 为什么cmake在64位系统上找到32位库而不是64位库? - Why is cmake finding 32-bit libraries instead of 64-bit libs on a 64-bit system? 在 64 位系统上使用 32 位共享库 - Using 32-bit shared libraries on 64-bit system 使用Visual Studio 2010的32位C ++到64位C ++ - 32-bit C++ to 64-bit C++ using Visual Studio 2010 在64位CPU上的C ++中的Mac OS X上,是否有64位的类型? - On Mac OS X in C++ on a 64-bit CPU, is there a type that is 64 bits? 为什么32位和64位系统上的“对齐”相同? - Why is the “alignment” the same on 32-bit and 64-bit systems?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM