• C++(十五) — sizeof 运算符


    1、基本数据类型

    sizeof 是一个关键字,它是一个编译时运算符,用于判断变量或数据类型的字节大小。

    sizeof 运算符可用于获取类、结构、共用体和其他用户自定义数据类型的大小。

    使用 sizeof 的语法如下:

    sizeof (data type)

    其中,data type 是要计算大小的数据类型,包括类、结构、共用体和其他用户自定义数据类型。

    请尝试下面的实例,理解 C++ 中 sizeof 的用法。复制并黏贴下面的 C++ 程序到 test.cpp 文件中,编译并运行程序。

    int main()
    {
        cout << "Size of char : " << sizeof(char) << endl;            //1
        cout << "Size of bool : " << sizeof(bool) << endl;            //1
    
        cout << "Size of int : " << sizeof(int) << endl;            //4
        cout << "Size of short int : " << sizeof(short int) << endl;//2
        cout << "Size of long int : " << sizeof(long int) << endl;    //4
        cout << "Size of long : " << sizeof(long) << endl;            //4
    
        cout << "Size of float : " << sizeof(float) << endl;        //4
        
        cout << "Size of double : " << sizeof(double) << endl;        //8
        cout << "Size of long double : " << sizeof(long double) << endl;//8
        cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;    //2
        system("pause");
        return 0;
    }

     2、字符串及字符数组

      数组作为函数的形参传递时,退化为指针。

    size_t getPtrSize(char *ptr)
    {
        return sizeof(ptr);
    }
    
    int main()
    {
        // hello 有 5 个字符,但总字节个数要额外加1个字节(结束符),所以是 6
        char szHello[] = "Hello";  //字符串    
        //字符数组
        char a[] = { 'h','e','l','l','o' };
        char b[10] = { 'h','e','l','l','o' };
        char c[2][3]= { 'h','e','l','l','o' };
    
        cout << "The size of a char is: "<< sizeof(char)                    // 1
            << "
    The length of " << szHello << " is: " << sizeof (szHello)    //6,字符串大小 = 本身字符 + ''
            << "
    The size of the pointer is "<< getPtrSize(szHello) << endl;//4 ,当64位机时,指针位8字节
    
        cout << "The size of a a array is: " << sizeof(a)                    //5
            << "
    The size of a b array is: " << sizeof(b)                //10
            << "
    The size of a c array is: " << sizeof(c)                //6 = 1*6
            << "
    The size of a c0 array is: " << sizeof(c[0])                //3 = 1*3 相当于一个一维数组
    
            << endl;
        system("pause");
    }

    3、指针

      32位机和64位机,指针大小分别为:4 和 8 。

    int main()
    {
        char a[3];
        cout << "The size of a a array is: " << sizeof(a)                //3,表示数组大小
            << "
    The size of a a array is: " << sizeof(a[0])            //1,表示数组的第一个字符占得大小
            << endl;
    
        // 数组指针,一个指针,指向有3个元素的一维数组,大小为一个指针的大小,4个字节
        // 执行 a1+1 时,要跨过3个 char 型字符长度的大小(相当于跨过这个数组指针的单元大小)
        char (*a1)[3]; 
        //*a1[0] = 6;
        cout << "
    The size of a a1 array is: " << sizeof(a1)                //4,表示指针
            << "
    The size of a a1 array is: " << sizeof(*a1[0])        //1,表示指针指向的字符数组的第0个字符
            << "
    The size of a a1 array is: " << sizeof(a1[0])            //3,表示指针指向的这个字符数组
            << endl;
    
        // 指针数组,有3个指针
        char *a2[3];
        cout << "
    The size of a a2 array is: " << sizeof(a2)           //12,表示 3*4=12
            << "
    The size of a a2 array is: " << sizeof(a2[0])            //4,表示第一个指针
    
            << endl;
    
        system("pause");
    }

    4、类

        类也是一种数据类型,也就是,固定大小内存块的别名。

      定义一个类后,是一个抽象概念,不分配内存,只有当定义了对象后才分配固定大小的内存。

      一个类所占的固定内存大小,计算时需要对齐补全。选择4的倍数,结构体内最长数据元素的整数倍。

      此外,类中定义的 static 变量是存在全局数据区内,不计算在类的内存中。

      类中成员函数,也不计算在内。(因为函数代码和变量,是分开存放的)

       比如:

    // 总共占用:4+4+4=12
    class A 
    {
        bool a;    //1
        int b;    //4
        bool c;    //1
    };
    
    // 总共占用:1+1=2(自动补全为4),,4+4=8
    class B
    {
        bool a;    //1
        bool b;    //1
        int c;    //4
    };
    
    int main()
    {
        A a1;
        B b1;
        cout << sizeof(a1) << endl;  //12
        cout << sizeof(b1) << endl;     //8
        system("pause");
        return 0;
    }

       另外,需要注意,对于空类:

    1.   定义空的类,实例化后,占用空间为1个字节;
    2.   多重继承还是1个字节;
    3.   虚继承有指针,是4个字节。

      之前看过几篇博客,都说是为了让对象能够有唯一的地址以区别不同的对象。这一个字节用来干什么的呢?里面的值是多少无所谓,因为这个字节只是用来占位置而已,正如上面打印的ab的地址一样,它们各自占用一个byte的内存,紧挨着。我们可以通过地址的不同来区别它们。

    其实这个问题本身没什么意义,只是探究C++对象内存模型的一个敲门砖。

    // 总共占用:1
    class A 
    {
        
    };
    // 多重继承还是:1
    class B :public A
    {
    
    };
    
    // 虚继承是:4,因为虚继承要有个虚表,这是个指针
    class C :virtual public A
    {
    
    };
    
    int main()
    {
        A a1;
        B b1;
        C c1;
        cout << sizeof(a1) << endl;  //1
        cout << sizeof(b1) << endl;     //1
        cout << sizeof(c1) << endl;     //4
        system("pause");
        return 0;
    }


    参考文献:https://blog.csdn.net/Jacketinsysu/article/details/52207285

  • 相关阅读:
    eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过
    MyEclipse中的查找快捷键
    eclipse和myeclipse怎么在项目中查找指定代码?https://www.jb51.net/softjc/554889.html
    Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
    Spring的应用上下文ApplicationContext
    springmvc中获取request对象,加载biz(service)的方法
    Spring中RequestContextHolder以及HandlerInterceptorAdapter的使用
    Spring 单元测试 RequestContextHolder.getRequestAttributes()).getRequest(); 为空的原因
    Lua整理——table库
    POJ 2377 Bad Cowtractors
  • 原文地址:https://www.cnblogs.com/eilearn/p/10124701.html
Copyright © 2020-2023  润新知