C++常见类型所占字节大小
来源:互联网 发布:淘宝试衣间在哪 编辑:程序博客网 时间:2024/06/06 06:53
(一)常见变量类型
常见的变量类型有char、short、int、long、float、double,与之相对应的还有几个无符号的类型,只需要前面加上unsigned即可,例如unsigned char、unsigned int等,另外还有些typedef出来的类型例如WORD、DWORD等,这些类型的大小一般说来与操做系统的位数有关(即32位/64位系统有关),实际上并非如此,常见的说法是与编译器、处理器、操作系统有关系,例如我测试的环境是Win7 X64位操作系统,理论上sizeof(long)的值应为8,但实际上仅为4,说明变量类型的大小不仅仅与操作系统的位数相关,还有一些其他的因素,下面是测试数据,和其中所遇到的问题。
(二)测试数据
- 测试环境:VS2008+Win7 x64
- 测试程序如下:
#include "stdafx.h"#include <windows.h>int _tmain(int argc, _TCHAR* argv[]){ printf("char size = %d\n", sizeof(char)); printf("unsigned char size = %d\n\n", sizeof(unsigned char)); printf("short size = %d\n", sizeof(short)); printf("unsigned short size = %d\n\n", sizeof(unsigned short)); printf("int size = %d\n", sizeof(int)); printf("unsigned int size = %d\n\n", sizeof(unsigned int)); printf("long size = %d\n", sizeof(long)); printf("unsigned long size = %d\n\n", sizeof(unsigned long)); printf("long long size = %d\n", sizeof(long long)); printf("unsigned long long size = %d\n\n", sizeof(unsigned long long)); printf("float size = %d\n", sizeof(float)); printf("long float size = %d\n\n", sizeof(long float)); printf("double size = %d\n", sizeof(double)); printf("long double size = %d\n\n", sizeof(unsigned long double)); printf("BYTE size = %d\n", sizeof(BYTE)); printf("WORD size = %d\n", sizeof(WORD)); printf("DWORD size = %d\n", sizeof(DWORD)); getchar(); return 0;}
- 测试结果如下:
(三)结果分析
- sizeof(char) = 1 这一点毋庸置疑,毕竟标准里说了这事,此外还有sizeof(short) <= sizeof(int) <=sizeof(long),至于他们每种类型究竟占用几个字节,还要看编译器的实现了,上图中的数据与我在win7 x32系统下的测试数据相同,也是32位系统常用的数据长度,这里说明操作系统的位数并不是决定类型长度的唯一因素,具体还要看编译器实现。
- 在测试中出现了BYTE、WORD、DWORD等类型,他们的定义分别是unsigned char、unsigned short、unsigned long,这些类型也是常常使用的,其定义在WinDef.h中,但是直接包含这个文件会出现一些依赖性的错误,所以最好还是包含windows.h文件,其中除了WinDef.h文件,还包含了windef.h、winbase.h、wingdi.h、winuser.h等及基础头文件。
- 测试例子可不是一帆风顺的,期间还出了一些警告和错误,先来说说警告:
c:\users\administrator\documents\visual studio 2008\projects\testmysql\testmysql\testmysql.cpp(25) : warning C4215:使用了非标准扩展 : 长浮点
这个警告看起来有意思,什么么叫长浮点,原来double类型相对于float类型来说就是长浮点类型,这里引用MSDN上的一段话来解释一下:默认 Microsoft 扩展 (/Ze) 将 long float 视为 double。ANSI 兼容性 (/Za) 则不是这样。请使用 double 以维护兼容性。下面的示例生成 C4215,
// C4215.cpp// compile with: /W1 /LDlong float a; // C4215// use the line below to resolve the warning// double a;
也就是说,既然有了double就不要再使用long float了。
- 除了警告我还弄出了2条错误,错误内容如下:
c:\users\administrator\documents\visual studio 2008\projects\testmysql\testmysql\testmysql.cpp(26) : error C2632:"__int64"后面的"float"非法c:\users\administrator\documents\visual studio 2008\projects\testmysql\testmysql\testmysql.cpp(30) : error C2632:"__int64"后面的"double"非法
这又是什么鬼,哪里来的__int64,后来看了一下行数,原来问题出现在long long上,在后面加上了float或者double以后就出现了这个问题,仔细想想longlong不就是__int64吗,原来是这样啊,谁知道当时怎们就突发奇想的写出了sizeof(long long float)……
- 除了这个错误我还写出了sizeof(unsigned float)这样滑稽的表达式、事实证明,在我所使用的环境下是无法通过编译的
- 这里漏了一种适应比较广泛的类型——指针,指针的大小是多大的,一般认为指针的大小与sizeof(long)相等,即32位系统占用4个字节,64位系统(编译器支持的前提下)占用8个字节。
0 0
- C++常见类型所占字节大小
- 常见基本类型所占字节数
- C++string所占字节大小
- C各种类型变量所占字节
- sizeof()函数求类型所占字节大小-指针,数组
- C语言中指针变量所占字节大小
- c语言中各类型所占字节大小
- 各个类型所占字节
- C/C++学习 - 基本类型所占字节数
- c/c++基本类型所占字节数
- C语言中各种整型类型所占字节数
- c/c++ 各个类型所占的字节数
- C语言类型数据所占字节数
- 各种类型所占大小
- JAVA对象所占字节大小计算方法
- C++ 数据类型所占字节大小
- 指针变量所占字节大小问题
- 判断int类型所占字节数
- 喜马拉雅听 手机端协议分析
- hdu 4672 Present Day, Present Time(博弈)
- 【MySQL】mysql中模糊查询的四种用法
- 使用oschina的git服务器图文流程
- hdu 4675 GCD of Sequence(计数)
- C++常见类型所占字节大小
- linux shell(2)
- 蓝懿ios 技术内容和心得交流分享 11.6
- Introduction to Algorithm - Summary of Chapter 7 - Quicksort
- iOS开发——高德地图调研之显示地图、定位与地理编码等
- MFC对话框屏幕居中
- ZigZag Convertion
- HDU 3724 Encoded Barcodes (2010 Asia Tianjin Regional Contest )
- 数据库性能优化的方法