sizeof

本文详细解析了C++中sizeof运算符的使用方法及其特点。通过多个示例代码展示了如何获取不同数据类型、数组、指针及引用的大小,并解释了在32位操作系统下基本数据类型的字节长度。

 // 扩展了 c++ primer中的代码,加了一些自己的注释
#include <iostream>
using namespace std;
int main() {
size_t ia;
    ia = sizeof( ia );     // ok
    ia = sizeof ia;        // ok
    // ia = sizeof int;    // error
    ia = sizeof( int );    // ok

    int *pi = new int[ 12 ]; // pi是指针变量,而不是数组名
    cout << "pi: " << sizeof( pi )
         << " *pi: " << sizeof( *pi )
         << endl;

    // a string's size is independent of
    // of the length of the string it addresses

    string st1( "foobar" );
    string st2( "a might oak" );
    string *ps = &st1;

    cout << "st1: " << sizeof( st1 )
         << " st2: " << sizeof( st2 )
         << " ps: " << sizeof( ps )
         << " *ps: " << sizeof( *ps )
         << endl;
    char *d_str = new char[3];
    d_str[0] = 'A';d_str[1] = 'b';d_str[2] = '/0';
    cout << "d_str:/t"<< sizeof(d_str) << endl; //4
    cout << "*d_str:/t"<< sizeof(*d_str) << endl;//1

    cout << "short :/t"    << sizeof(short)    << endl; //2
    cout << "short* :/t"   << sizeof(short*)   << endl; //4
    cout << "short& :/t"   << sizeof(short&)   << endl; //2
    cout << "short[3] :/t" << sizeof(short[3]) << endl; //6

    int arr[] ={4,8,9,5,6}; // ia不可改变,可用数组名求其数组大小
    int *parr = arr;
    cout << "arr :/t" << sizeof(arr) << endl; // 20,是数组名,而不是指针
    cout << "parr :/t"<< sizeof(parr) << endl; // 变量,只能求其指针大小//4
    cout << "*parr :/t"<<sizeof(*parr) << endl; // 4

    char str_c[] ={"china"};
    char *p_str = str_c;
    cout << "str_c" << sizeof(str_c) << endl; //6 字符串也按数组计算大小
    cout << "p_str" << sizeof(p_str) << endl; //4
    cout << "*p_str"<< sizeof(*p_str) << endl; // 1


}

 

@font-face{ font-family:"Times New Roman"; } @font-face{ font-family:"宋体"; } @font-face{ font-family:"Symbol"; } @font-face{ font-family:"Arial"; } @font-face{ font-family:"黑体"; } @font-face{ font-family:"Courier New"; } @font-face{ font-family:"Wingdings"; } @font-face{ font-family:"新宋体"; } @font-face{ font-family:"Courier New CYR"; } p.0{ margin:0pt; margin-bottom:0.0001pt; layout-grid-mode:char; text-align:justify; font-size:10.5000pt; font-family:'Times New Roman'; } div.Section0{ margin-top:72.0000pt; margin-bottom:72.0000pt; margin-left:90.0000pt; margin-right:90.0000pt; size:612.0000pt 792.0000pt; }

在32位操作系统中,基本数据类型 

类型                  字节长度 

char                    1 

short                    2 

short    int            2 

signed short            2 

unsigned short          2 

int                      4 

long    int            4 

signed  int            4 

unsigned int(unsigned)  4 

long                    4 

unsigned long            4 

float                    4 

double                  8 

void*                    4 (所有指针类型长度都一样)(char*,int*,float*,double*) 

enum                    4 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值