C/C++:sizeof(short)、sizeof(int)、sizeof(long)、sizeof(long long) 等......在 32 位机器上与在 64 位机器上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20109984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C/C++: sizeof(short), sizeof(int), sizeof(long), sizeof(long long), etc... on a 32-bit machine versus on a 64-bit machine
提问by John Sonderson
I'm running Windows 7 (64-bit).
我正在运行 Windows 7(64 位)。
This question looks at the same question found here:
这个问题着眼于在此处找到的相同问题:
but is more in-depth as it deals with even more data types and applies to C or C++, not C#. First of all, I am using Microsoft Visual Studio Ultimate 2012. Unfortunately, while this IDE supports C# and Visual C++ it no longer supports plain old Visual C it seems. Anyhow, I've tried the creating the following standard C++ program in the IDE:
但更深入,因为它处理更多数据类型并适用于 C 或 C++,而不是 C#。首先,我使用的是 Microsoft Visual Studio Ultimate 2012。不幸的是,虽然这个 IDE 支持 C# 和 Visual C++,但它似乎不再支持普通的旧 Visual C。无论如何,我已经尝试在 IDE 中创建以下标准 C++ 程序:
#include <cstdio>
int main(int argc, char **argv) {
printf("sizeof(short): %d\n", (int) sizeof(short));
printf("sizeof(int): %d\n", (int) sizeof(int));
printf("sizeof(long): %d\n", (int) sizeof(long));
printf("sizeof(long long): %d\n", (int) sizeof(long long));
printf("sizeof(size_t): %d\n", (int) sizeof(size_t));
printf("sizeof(void *): %d\n", (int) sizeof(void *));
printf("Hit enter to exit.\n");
char *scannedText;
scanf("%s", &scannedText);
return 0;
}
and since I couldn't find the option to run a console application I simply placed a breakpoint at the "return 0;" statement, so as to view the output in the console. The result was:
由于我找不到运行控制台应用程序的选项,我只是在“return 0;”处放置了一个断点。语句,以便在控制台中查看输出。结果是:
sizeof(short): %d\n", 4
sizeof(int): %d\n", 4
sizeof(long): %d\n", 4
sizeof(long long): 8
sizeof(size_t): 4
sizeof(void *): 4
Hit enter to exit.
Old C textbooks state that int is set to the "word size", which is 16 on 16-bit machines and 32 on 32-bit machines. However this rule seems to break on 64-bit systems where one would expect the "word size" to be 64. Instead, from what I've read these systems are like 32-bit systems but have better support for 64-bit computations than their 32-bit counterparts did. Hence, the results obtained from the above C++ program are exactly the same as one would obtain on a 32-bit system. The size of data types (size_t) (which can be used to measure amount of memory taken up by objects in memory) also stores its values in 4 bytes, and it is also interesting that the size of pointers used to access memory locations (for instance sizeof(void *) shows the number of bits used to store generic pointers to any location in memory) is also 32 bits long.
旧的 C 教科书声明 int 设置为“字长”,在 16 位机器上为 16,在 32 位机器上为 32。然而,这条规则似乎在 64 位系统上打破了,人们期望“字长”为 64。相反,从我读过的内容来看,这些系统类似于 32 位系统,但对 64 位计算的支持比他们的 32 位同行做到了。因此,从上述 C++ 程序获得的结果与在 32 位系统上获得的结果完全相同。数据类型的大小(size_t)(可用于衡量对象在内存中占用的内存量)也将其值存储在 4 个字节中,
Anyone know how come Visaul C was removed from Visual Studio 2012 and whether it is still possible to run console applications from Visual Studio 2012 without having to set a breakpoint or read text from standard input prior to exiting as above in order for the console window to pause before closing?
任何人都知道 Visaul C 是如何从 Visual Studio 2012 中删除的,以及是否仍然可以从 Visual Studio 2012 运行控制台应用程序,而无需在退出之前设置断点或从标准输入读取文本,以便控制台窗口能够关闭前暂停?
Furthermore, is my interpretation correct, or do I have something misconfigured in the IDE so that, for instance, it compiles for 32-bit rather than for 64-bit systems? According to one of the poster, since my system is 64-bit, I should see the results described here for size_t and pointers: https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_modelsbut I am not seeing this. Is there a way to reconfigure Visual Studio so that it may support a 64-bit memory model, as opposed to what I am currently seeing in the program's output?
此外,我的解释是否正确,还是我在 IDE 中配置错误,例如,它针对 32 位而不是 64 位系统进行编译?根据其中一张海报,由于我的系统是 64 位,我应该看到这里描述的 size_t 和指针的结果:https: //en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models但我不是看到这个。有没有办法重新配置 Visual Studio,使其支持 64 位内存模型,而不是我目前在程序输出中看到的?
Thanks.
谢谢。
回答by Dweeberly
Looks right to me. In c/c++ int isn't specifically defined in terms of bit-size. When creating a project you can select a "console application". VS2012 still supports C, but they mostly lump projects into C/C++. There is a compiler option (/TC I think) which will force the compiler into C compliance. By default it will imply the language by the file extension. MS C support isn't ideal, it doesn't include stdbool.h for instance.
对我来说看起来很合适。在 c/c++ 中 int 没有根据位大小专门定义。创建项目时,您可以选择“控制台应用程序”。VS2012 仍然支持 C,但它们大多将项目合并为 C/C++。有一个编译器选项(我认为是 /TC),它会强制编译器符合 C 语言。默认情况下,它将通过文件扩展名暗示语言。MS C 支持并不理想,例如它不包括 stdbool.h。
If you want to control the bit size of your data you can use stdint.h which contains exact width int datatypes.
如果要控制数据的位大小,可以使用包含精确宽度 int 数据类型的 stdint.h。