Sizeof empty class

前端 未结 3 808
情歌与酒
情歌与酒 2021-01-12 11:31

With the code:

#include 

class A {};
class B { char x; };

int main()
{
    std::cerr << sizeof(A) << \" \" << sizeof(B) &         


        
3条回答
  •  暖寄归人
    2021-01-12 12:08

    There is no requirement in the C++ standard that an empty object should have one byte of memory occupied. It is purely based on the implementation.

    EDIT: true, it's conforming ( ISO/IEC 14882 p.149 ):

    9 Classes [class]
    ..
    ..
    ..
    3 Complete objects and member subobjects of class type shall have nonzero size ...

提交回复
热议问题