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

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

问题描述

我正在运行Windows 7(64位)。



此问题与在此找到的问题相同:



在64位计算机上很长



,但是它更深入,因为它处理甚至更多的数据类型,并将
应用于C或C ++,而不是C#。首先,我正在使用Microsoft Visual Studio Ultimate2012。
不幸的是,尽管此IDE支持C#和Visual C ++,但它似乎不再支持普通的
旧VisualC。无论如何,我尝试在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);

返回0;

}

,由于找不到运行a的选项控制台应用程序,我只是
在返回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
按Enter退出。

旧C教科书指出int设置为字长,在16位
计算机上为16,在32位计算机上为32,但是在64位
系统上,该规则似乎中断了字长为64。相反,我从
那里读到的这些系统就像32位系统,但是对
64位计算的支持要比其32位系统好。 ,从上述C ++程序获得的结果
与在32位系统上获得的结果
完全相同。数据类型(size_t)的大小(可以将
用于测量内存中对象占用的内存量)
还将其值存储在4个字节中,并且有趣的是
的大小用于访问备忘录ry位置(例如sizeof(void *)
显示用于将通用指针存储到内存中任何位置的位数)
也是32位长。



任何人都知道Visaul C是如何从Visual Studio 2012中删除的,以及是否
是否仍然可以从Visual Studio 2012运行控制台应用程序而无需
设置断点或从标准中读取文本

此外,我的解释是正确的,还是我配置错误?在IDE中使用b $ b,例如,它是为32位而不是64位
系统编译的?根据发布者之一的说法,由于我的系统是64位的,因此我应该
看到此处针对size_t和指针描述的结果:
https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models
但我没有看到。有没有一种方法可以重新配置Visual Studio
,使其可以支持64位内存模型,而不是我在程序输出中当前看到的
? b $ b

谢谢。

解决方案

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



如果要控制数据的位大小,可以使用stdint。 h,其中包含确切的宽度int数据类型。


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

This question looks at the same question found here:

long on a 64 bit machine

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\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:

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.

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?

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?

Thanks.

解决方案

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.

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

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

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