C/C++:sizeof(short)、sizeof(int)、sizeof(long)、sizeof(long long) 等......在 32 位机器上与在 64 位机器上 [英] C/C++: sizeof(short), sizeof(int), sizeof(long), sizeof(long long), etc... on a 32-bit machine versus on a 64-bit machine

查看:51
本文介绍了C/C++:sizeof(short)、sizeof(int)、sizeof(long)、sizeof(long long) 等......在 32 位机器上与在 64 位机器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的是 Windows 7(64 位).

I'm running Windows 7 (64-bit).

这个问题着眼于在此处找到的相同问题:

This question looks at the same question found here:

在 64 位机器上长

但更深入,因为它处理更多的数据类型并适用到 C 或 C++,而不是 C#.首先,我使用的是 Microsoft Visual Studio Ultimate 2012.不幸的是,虽然这个 IDE 支持 C# 和 Visual C++,但它不再支持普通的似乎是旧的 Visual C.无论如何,我已经尝试创建以下标准 C++IDE 中的程序:

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:

#include <cstdio>

int main(int argc, char **argv) {

  printf("sizeof(short): %d
", (int) sizeof(short));

  printf("sizeof(int): %d
", (int) sizeof(int));

  printf("sizeof(long): %d
", (int) sizeof(long));

  printf("sizeof(long long): %d
", (int) sizeof(long long));

  printf("sizeof(size_t): %d
", (int) sizeof(size_t));

  printf("sizeof(void *): %d
", (int) sizeof(void *));

  printf("Hit enter to exit.
");

  char *scannedText;

  scanf("%s", &scannedText);

  return 0;

}

而且由于我找不到运行控制台应用程序的选项,我只是在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:

sizeof(short): %d
", 4
sizeof(int): %d
", 4
sizeof(long): %d
", 4
sizeof(long long): 8
sizeof(size_t): 4
sizeof(void *): 4
Hit enter to exit.

旧的 C 教科书指出 int 设置为字长",即 16 位上的 16机器和 32 位机器上的 32.然而,这条规则似乎在 64 位上打破了人们期望字长"为 64 的系统.相反,从什么我读过这些系统就像 32 位系统,但有更好的支持64 位计算比他们的 32 位对应.因此,结果从上面的 C++ 程序中获得的结果和你想得到的完全一样在 32 位系统上获取.数据类型的大小(size_t)(可以用于测量内存中对象占用的内存量)还将其值存储在 4 个字节中,而且有趣的是用于访问内存位置的指针大小(例如 sizeof(void *)显示用于存储指向内存中任何位置的通用指针的位数)也是 32 位长.

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.

谁知道 Visaul C 是如何从 Visual Studio 2012 中删除的?仍然可以从 Visual Studio 2012 运行控制台应用程序,而无需在退出之前必须设置断点或从标准输入读取文本如上所述,为了让控制台窗口在关闭前暂停?

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?

此外,我的解释是否正确,或者我是否配置错误例如,在 IDE 中编译为 32 位而不是 64 位系统?根据其中一位海报,由于我的系统是 64 位,我应该请参阅此处描述的 size_t 和指针的结果:https://en.wikipedia.org/wiki/64-bit_computing#64-位数据模型但我没有看到这个.有没有办法重新配置 Visual Studio以便它可以支持 64 位内存模型,而不是什么我目前在程序的输出中看到了吗?

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_models but 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?

谢谢.

推荐答案

在我看来是正确的.在 c/c++ 中 int 没有根据位大小专门定义.创建项目时,您可以选择控制台应用程序".VS2012 仍然支持 C,但它们大多将项目合并为 C/C++.有一个编译器选项(我认为是/TC),它将强制编译器符合 C 语言.默认情况下,它将通过文件扩展名暗示语言.MS C 支持并不理想,例如它不包括 stdbool.h.

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.

如果您想控制数据的位大小,可以使用包含精确宽度 int 数据类型的 stdint.h.

If you want to control the bit size of your data you can use stdint.h which contains exact width int datatypes.

这篇关于C/C++:sizeof(short)、sizeof(int)、sizeof(long)、sizeof(long long) 等......在 32 位机器上与在 64 位机器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆