• c++ 空类的大小


      直接看代码:

    1. #include
    2. #include
    3. class CEmpty
    4. {
    5. public:
    6. CEmpty()
    7. {
    8. }
    9. ~CEmpty()
    10. {
    11. }
    12. };
    13. int main()
    14. {
    15. CEmpty emptyClass;
    16. printf("sizeof CEmpty is: %lu\n", sizeof(emptyClass));
    17. return 0;
    18. }

    1,首先什么样的才是空类呢?

    它是一个不包含任何数据成员的类(例如int a、float b、char c和string d等)。然而,空类可能包含成员函数。

     2,为什么空类的大小是 1 byte 呢?

    简单地说,类只有在实例化的时候才会分配内存空间,即定义变量时(栈上定义或堆上定义),那分配内存空间肯定要有大小,而空类的大小是由编译器定的,这样一个类定义多个对象时,能保证每个对象的地址都是唯一的。如:

    1. #include
    2. #include
    3. class CEmpty
    4. {
    5. public:
    6. CEmpty()
    7. {
    8. }
    9. ~CEmpty()
    10. {
    11. }
    12. };
    13. int main()
    14. {
    15. printf("sizeof CEmpty is: %lu\n", sizeof(CEmpty));
    16. CEmpty emptyClass1, emptyClass2;
    17. if(&emptyClass1 == &emptyClass2)
    18. {
    19. printf("two object address are same\n");
    20. }
    21. else
    22. {
    23. printf("two object address are not same\n");
    24. }
    25. return 0;
    26. }

     看下面的代码,一个派生类继承于一个空基类,其大小是多少呢?

    1. #include
    2. #include
    3. class CEmpty
    4. {
    5. public:
    6. CEmpty()
    7. {
    8. }
    9. ~CEmpty()
    10. {
    11. }
    12. };
    13. class Derived: public CEmpty
    14. {
    15. private:
    16. int mValue;
    17. };
    18. int main()
    19. {
    20. printf("sizeof CEmpty is: %lu\n", sizeof(CEmpty));
    21. CEmpty emptyClass1, emptyClass2;
    22. if(&emptyClass1 == &emptyClass2)
    23. {
    24. printf("two object address are same\n");
    25. }
    26. else
    27. {
    28. printf("two object address are not same\n");
    29. }
    30. Derived derive;
    31. printf("sizeof derive is: %lu\n", sizeof(derive));
    32. return 0;
    33. }

     这里有个有趣的规则 :

    Note: The output is not greater than 4. There is an interesting rule that says that an empty base class need not be represented by a separate byte. So compilers are free to make optimization in case of empty base classes. 

    我们知道类的大小是不包括成员函数及static成员数据的,为什么呢?我想应该是:成员函数和static成员是属于类层面的,它不属于对象,那对象是什么呢?对象就是定义的变量,而非static成员数据才是每个对象的东西,它们是对象的一部分。可能对初学者不好理解,下面一个例子也许可以帮助理解:

    1. #include
    2. #include
    3. class CEmpty
    4. {
    5. public:
    6. CEmpty()
    7. {
    8. }
    9. ~CEmpty()
    10. {
    11. }
    12. void doNothing()
    13. {
    14. printf("nothing to do\n");
    15. }
    16. static int mValue;
    17. };
    18. int main()
    19. {
    20. printf("sizeof CEmpty is: %lu\n", sizeof(CEmpty));
    21. CEmpty *test = NULL;
    22. test->doNothing();
    23. return 0;
    24. }

    定义了一个 CEmpty 的指针且赋值为NULL,第一眼看上去觉得 test->doNothing(); 肯定有问题,NULL 指针怎么能这样用呢?而结果呢?

     为什么不会崩溃?

    第一是函数关不属于对象,不一定要有效的对象才能调用;第二是this指针,因为这个函数里没有引用到成员数据,假如有引用成员数据必定崩溃的。可以用 objdump 查看 a.out, 函数其实是存放在文本区(.text)的。

     注意:然而在 c 语言中,空的结构体的大小是 0,目前找到的原因是:在C语言中引入结构时,当时还没有对象的概念。因此,根据C标准,决定将空结构的大小保持为 0。

  • 相关阅读:
    Leetcode 35. 搜索插入位置(二分)
    舆情监控究竟是什么?怎么运作的?
    【动态规划】leetcode 509. 斐波那契数
    XctNet:从单个X射线图像重建体积图像的网络
    Google Chrome(谷歌浏览器)安装使用
    cup处理器的编号详情
    MIPI CSI-2笔记(11) -- Low Level Protocol(统一串行链路,Unified Serial Link)
    【JavaScript基础】BOM介绍
    【Python刷题篇】——Python入门 09 字典(下)
    如何评价微软发布的Phi-3,手机都可以运行的小模型
  • 原文地址:https://blog.csdn.net/tianyexing2008/article/details/126162277