现在的位置: 首页 > 综合 > 正文

sizeof

2018年08月31日 ⁄ 综合 ⁄ 共 2353字 ⁄ 字号 评论关闭

 // 扩展了 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 

【上篇】
【下篇】

抱歉!评论已关闭.