实验: 内建数据类型的size

原创 2011年01月14日 08:15:00
// test1020.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>

int fnCalculateBuildInDataSize();

int _tmain(int argc, _TCHAR* argv[])
{
    fnCalculateBuildInDataSize();

    getwchar();
    return 0;
}

int fnCalculateBuildInDataSize()
{  
    printf("size of BOOL = %d[Bytes]\r\n", sizeof(BOOL));  
    printf("size of CHAR : %d[Bytes]\r\n", sizeof(CHAR));  
    printf("size of BYTE : %d[Bytes]\r\n", sizeof(BYTE));  
    printf("size of SHORT : %d[Bytes]\r\n", sizeof(SHORT));  
    printf("size of INT : %d[Bytes]\r\n", sizeof(INT));  
    printf("size of LONG : %d[Bytes]\r\n", sizeof(LONG));  
    printf("size of ULONG : %d[Bytes]\r\n", sizeof(ULONG));  
    printf("size of DWORD : %d[Bytes]\r\n", sizeof(DWORD));  
    printf("size of FLOAT : %d[Bytes]\r\n", sizeof(FLOAT));  
    printf("size of DOUBLE : %d[Bytes]\r\n", sizeof(DOUBLE));  
    printf("size of PVOID : %d[Bytes]\r\n", sizeof(PVOID));  
    printf("\r\n");  
    printf("size of bool = %d[Bytes]\r\n", sizeof(bool));  
    printf("size of char : %d[Bytes]\r\n", sizeof(char));  
    printf("size of unsigned char : %d[Bytes]\r\n", sizeof(unsigned char));  
    printf("size of short int : %d[Bytes]\r\n", sizeof(short int));  
    printf("size of int : %d[Bytes]\r\n", sizeof(int));  
    printf("size of long : %d[Bytes]\r\n", sizeof(long));  
    printf("size of unsigned long : %d[Bytes]\r\n", sizeof(unsigned long));  
    printf("size of float : %d[Bytes]\r\n", sizeof(float));  
    printf("size of double : %d[Bytes]\r\n", sizeof(double));  

    //printf("size of void : %d[Bytes]\r\n", sizeof(void));//void 不能用sizeof来计算size   
    printf("size of void * : %d[Bytes]\r\n", sizeof(void *));  

    /** 运行结果 relase by x86
    size of BOOL = 4[Bytes]
    size of CHAR : 1[Bytes]
    size of BYTE : 1[Bytes]
    size of SHORT : 2[Bytes]
    size of INT : 4[Bytes]
    size of LONG : 4[Bytes]
    size of ULONG : 4[Bytes]
    size of DWORD : 4[Bytes]
    size of FLOAT : 4[Bytes]
    size of DOUBLE : 8[Bytes]
    size of PVOID : 4[Bytes]

    size of bool = 1[Bytes]
    size of char : 1[Bytes]
    size of unsigned char : 1[Bytes]
    size of short int : 2[Bytes]
    size of int : 4[Bytes]
    size of long : 4[Bytes]
    size of unsigned long : 4[Bytes]
    size of float : 4[Bytes]
    size of double : 8[Bytes]
    size of void * : 4[Bytes]
    */  

    /** 运行结果 relase by x64
    size of BOOL = 4[Bytes]
    size of CHAR : 1[Bytes]
    size of BYTE : 1[Bytes]
    size of SHORT : 2[Bytes]
    size of INT : 4[Bytes]
    size of LONG : 4[Bytes]
    size of ULONG : 4[Bytes]
    size of DWORD : 4[Bytes]
    size of FLOAT : 4[Bytes]
    size of DOUBLE : 8[Bytes]
    size of PVOID : 8[Bytes]

    size of bool = 1[Bytes]
    size of char : 1[Bytes]
    size of unsigned char : 1[Bytes]
    size of short int : 2[Bytes]
    size of int : 4[Bytes]
    size of long : 4[Bytes]
    size of unsigned long : 4[Bytes]
    size of float : 4[Bytes]
    size of double : 8[Bytes]
    size of void * : 8[Bytes]
    */  

    return S_OK;  
}

/**
实验结论:
    * 不管是哪种平台的PE, double size = 8Bytes

    * X64PE, void * size = 8Bytes
    * X86PE, void * size = 4Bytes
 
    * BOOL 是宏, size = 4
    * bool 是内建数据类型 size = 1
*/


 

    nSize1 = sizeof(PVOID64);   ///< x86, x64下都是8
    nSize2 = sizeof(PVOID);     ///< x86下是4, x64下是8



 

<2013_0529>

sizeof(ULONGLONG) = 8
sizeof(INT64) = 8

<2013_0902>

// testMacro.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
#ifdef _X86_
    _tprintf(L"x86 : DWORD size = %d\r\n", sizeof(DWORD));
    _tprintf(L"x86 : DWORD_PTR size = %d\r\n", sizeof(DWORD_PTR));
#else ifdef (_IA64_ || _AMD64_)
    _tprintf(L"x64 : DWORD size = %d\r\n", sizeof(DWORD));
    _tprintf(L"x64 : DWORD_PTR size = %d\r\n", sizeof(DWORD_PTR));
#endif

    _tprintf(L"\r\nEND, press anykey to quit\r\n");

    getwchar();

    /// run results
    /// x86 : DWORD size = 4
    /// x86 : DWORD_PTR size = 4

    /// x64 : DWORD size = 4
    /// x64 : DWORD_PTR size = 8
    return 0;
}

<2017-10-10>

在gcc-elf64下, sizeof(long) or sizeof(unsigned long) = 8, 而不是4.

在vs下不管x86, x64, sizeof(long) or sizeof(unsigned long) = 4, 都是4.


 

版权声明:

相关文章推荐

非内建数据类型的编译器对其规则

非内建数据:用户自己定义的数据类型 比如,结构体,联合体,类 C/C++数据对齐 为了避免混淆,做如下规定,以下代码若不加特殊说明都运行于32位平台,结构体的默认对齐值是8,各数...

c/c++: uint8_t uint16_t uint32_t uint64_t size_t ssize_t数据类型

原文写的不错,转来收藏,转自:http://wangyisouhuxin.blog.163.com/blog/static/761966592011072348700/?fromdm&fromSear...

C语言第十七篇:size_t 数据类型

size_t

c/c++: uint8_t uint16_t uint32_t uint64_t size_t ssize_t数据类型

在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等。咋一看,好像是个新的数据类型,不过C语言(nesc是C的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样的...

实验三 数据类型,运算符

  • 2012年05月05日 17:15
  • 43KB
  • 下载

OpenCv学习笔记(三)---OpenCv中基本数据类型--Point,Size,Rect,Scalar,Vec3b类类型的详细解释及其OpenCv中源代码的详细分析

/********************************************************************************************* 程序功能:...
  • maweifei
  • maweifei
  • 2016年04月23日 17:23
  • 18049

关于引用构造函数形参和实际参数的数据类型必须一致的实验案例

在构造函数后引用函数的话实际的参数必须和构造函数时的形参数据类型一致,为此笔者做了一个实验,代码如下: 笔者首先构造了一个求绝对值的函数zabs(),然后将整型变量b传入函数,计算其绝对值,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:实验: 内建数据类型的size
举报原因:
原因补充:

(最多只允许输入30个字)