通过程序解读sizeof

<pre name="code" class="cpp">#include <iostream>
#include <string>

using namespace std;

void foo3(char *c1)
{
    int c3 = sizeof( c1 ); // c3 ==
    cout<< c3 << endl;
}
void foo4(int *c2)
{
    int c4 = sizeof( c2 ); // c4 ==
    cout<< c4 << endl;
}

//字节对齐的细节和编译器实现相关,但一般而言,满足三个准则:
//1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除;
//2) 结构体每个成员相对于结构体首地址的偏移量(offset)都是成员大小的整数倍,如有需要编译器会在成员之间加上填充字节;
//3) 结构体的总大小为结构体最宽基本(内置)类型成员大小的整数倍,如有需要编译器会在最末一个成员之后加上填充字节.
struct student
{
    int num; // 占4个字节
    char name[20]; // 占20个字节
    char sex; // 占4个字节(结构体为每个成员分配空间时,根据最宽基本类型成员大小的整数倍进行分配,这里指的是int/float)
    double age; // 占4字节
    float score; // 占4字节
    char addr[30]; // 占32字节(分配的空间必须为最宽基本类型成员大小的整数倍)
};

struct S1
{
    char a; // 4字节
    int i; // 4字节
};

// 嵌套的结构体
struct S2
{
    char a; // 4字节
    int i; // 4字节
    student t; // 占68字节,最宽基本类型成员为int/float型,4字节(注意,这里最大成员不是t,因为它不属于内置类型;具体计算S2时,student要按S2中的最近倍数来算)
};

// 空结构体
struct S3{};

// 共用体成员共用一个空间,所以其为最宽基本类型成员的大小
union U
{
    int i;
    char a;
};

int main()
{
    /* 1.
    int count=0; int m=779;
    while(m)
    {
        count++; m=m&(m-1);
    }
    //printf("%d\n",count);
    cout<< count << endl;
    return 0;
    */

    // sizeof的用法
    //(1)数组1
    int p1[10];
    int *p2[10];
    int (*p3)[10];


    cout<< "int p1[10]       sizeof(p1):" << sizeof(p1) << endl;
    cout<< "int *p2[10]      sizeof(p2):" << sizeof(p2) << endl;
    cout<< "int (*p3)[10]    sizeof(p3):" << sizeof(p3) << endl;
    cout<< "----------------------------"<< endl;

    //(2)内置类型
    char a1;
    int a2;
    float a3;
    double a4;
    long a5;
    long double a6;
    string a7;
    string a8="abc"; // string类型变量本质上属于指针

    cout<< "char:            " << sizeof(a1) << endl;
    cout<< "int:             " << sizeof(a2) << endl;
    cout<< "float:           " << sizeof(a3) << endl;
    cout<< "double:          " << sizeof(a4) << endl;
    cout<< "long:            " << sizeof(a5) << endl;
    cout<< "long double:     " << sizeof(a6) << endl;
    cout<< "string:          " << sizeof(a7) << endl;
    cout<< "string a8=\"abc\": " << sizeof(a8) << endl;
    cout<< "----------------------------"<< endl;


    //(3)指针:所有的指针(包括结构体)均占4字节(一个int型大小)。
    char *b1;
    int *b2;
    float *b3;
    double *b4;
    long *b5;
    long double *b6;

    cout<< "char*:       " << sizeof(b1) << endl;
    cout<< "int*:        " << sizeof(b2) << endl;
    cout<< "float*:      " << sizeof(b3) << endl;
    cout<< "double*:     " << sizeof(b4) << endl;
    cout<< "long*:       " << sizeof(b5) << endl;
    cout<< "long double*:" << sizeof(b6) << endl;
    cout<< "----------------------------"<< endl;

    //(4)数组2
    char c1[]="abcd";
    int c2[4];

    cout<< "char c1[]=\"abcd\":  " << sizeof(c1) << endl;
    cout<< "int c2[4]:        " << sizeof(c2) << endl;

    //数组名做形参:本质就是指针
    cout<< "foo3(char *c1):   ";
    foo3(c1);
    cout<< "foo3(int *c2):    ";
    foo4(c2);
    cout<< "----------------------------"<< endl;

    // (5)结构体
    S1 s1;
    S1 *s2;
    S2 s3;
    S3 s4;
    student student1;
    student *student2;

    cout<< "S1 s1:             " << sizeof(s1) << endl;
    cout<< "S1 *s2:            " << sizeof(s2) << endl;
    cout<< "student student1:  " << sizeof(student1) << endl;
    cout<< "student *student2: " << sizeof(student2) << endl;
    cout<< "S2 s3:             " << sizeof(s3) << endl;
    cout<< "S3 s4:             " << sizeof(s4) << endl;
    cout<< "----------------------------"<< endl;

    // 共用体
    U u;
    cout<< "U u:               " << sizeof(u) << endl;

	system("pause");

    return 0;
}

<div id="art_demo">本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节</div><p>在C语言中我们操作字符串肯定用到的是指针或者数组,这样相对来说对字符串的处理还是比较麻烦的,好在C++中提供了 string 类型的支持,让我们在处理字符串时方便了许多。</p><p><strong><span style="COLOR: #ff0000">首先,我写了一段测试代码,如下所示:
</span></strong></p><pre name="code" class="cpp">#include <iostream>
using namespace std;
int main(void)
{
 string str_test1;
 string str_test2 = "Hello World";
 int value1, value2, value3;
 value1 = sizeof(str_test1);
 value2 = sizeof(str_test2);
 value3 = sizeof(string);
 cout<<"str_test1占 "<<value1<<" 个字节"<<endl;
 cout<<"str_test2占 "<<value2<<" 个字节"<<endl;
 cout<<"string占 "<<value3<<" 个字节"<<endl;
 system("pause");
 return 0;
}
首先,我用G++编译运行,得到的结果如下图所示:

结果都是4字节;
<p>这说明string占4个字节。
之后,我用VS2012编译运行,得到的结果如下图所示:
结果都是28字节。</p><p>奇怪,这里string竟然占28个字节。
这里,我们注意观察,还会发现一个问题,不管有没有对string类型的变量赋值,或者是赋什么值,得到的结果是一样的。</p><p><strong>下面,来解释上述问题:
</strong>string的实现在各库中可能有所不同,但是在同一库中相同一点是,无论你的string里放多长的字符串,它的sizeof()都是固定的,字符串所占的空间是从堆中动态分配的,与sizeof()无关。    sizeof(string)=4可能是最典型的实现之一,不过也有sizeof()为12、32字节的库实现。通常,我们所用到的 string 类型一般都会是这样实现:
</p><p><pre name="code" class="cpp">class{      
char *_Ptr;    //指向字符串的指针      
int _Len;      //字符串的长度      
........};



所以,我们一般接触到的string类型所占字节数为 8+。






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值