【基础常识】常见变量类型在内存中占用大小

在这一篇,咱们弄清楚一些常见变量类型在内存中的占用

/*
----------------------------------------------------------------------------- 
OS:Win10 x86_64  
IDE:VS2012 Ultimate   win32控制台                                         
Author: Y. S. Cui 
----------------------------------------------------------------------------- 
*/


什么时候用 

  • 首先,必须有这个意识:指针长度和地址总线有关。因为指针记录的就是一个地址,那么32位的就是4字节,64位的就是8字节。
  • #include <iostream>  
    //#include <vector>  
    using namespace std;  
      
    int main()  
    {  
        double *p = nullptr;  
        cout << " 变量类型 char  的大小为" << sizeof(char)   << " Byte(s)" <<endl;  
        cout << " 变量类型 short 的大小为" << sizeof(short)  << " Byte(s)" <<endl;  
        cout << " 变量类型 double的大小为" << sizeof(double) << " Byte(s)" <<endl;  
      
        cout << " 变量类型 int   的大小为" << sizeof(int)   << " Byte(s)" <<endl;  
        cout << " 变量类型 float 的大小为" << sizeof(float) << " Byte(s)" <<endl;  
        cout << " 变量类型 long int 的大小为" << sizeof(long int) << " Byte(s)" <<endl;  
    
    
        cout << " 变量类型 long  的大小为" << sizeof(long)  << " Byte(s)" <<endl;  
        cout << " 变量类型 long long  的大小为" << sizeof(long long)  << " Byte(s)" <<endl;  
        cout << " 变量类型 unsigned long long  的大小为" << sizeof(unsigned long long)  << " Byte(s)" <<endl;  
    
    
    
    
        cout << " 指针char类型   的大小为" << sizeof(char *)<< " Byte(s)" <<endl;  
        system("pause");  
        return 0;  
    }  



#include <iostream>
#include <string>

#define unsigned char unchar
#define unsigned int uint 
using namespace std;

int main()
{
	cout << "   类型名称   \t" << "大小"  << endl;

	cout << "   bool       \t" << sizeof(bool)      << endl;
	cout << "   char       \t" << sizeof(char)      << endl;
	cout << "   wchar_t    \t" << sizeof(wchar_t)   << endl;
	cout << "   char16_t   \t" << sizeof(char16_t)  << endl;
	cout << "   char32_t   \t" << sizeof(char32_t)  << endl;
	cout << "   short      \t" << sizeof(short)     << endl;
	cout << "   int        \t" << sizeof(int)       << endl;
	cout << "   long       \t" << sizeof(long)      << endl;
	cout << "   long long  \t" << sizeof(long long) << endl;
	cout << "   float      \t" << sizeof(float)     << endl;
	cout << "   double     \t" << sizeof(double)    << endl;
	cout << "   long double\t" << sizeof(long double)     << endl;

	system("pause");
	return 0;
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值