Some knowledge points about struct

1. Layout

When allocating memory space for members, the order of allocation is consistent with the order of declaring the structure, as shown in the following structure:

struct ceshi
{
    char frist;
    int second;
    char three;
};

The layout in memory is like this ( type size ):

The members in descending order can reduce the waste of space:

The layout in memory at this time is like this:

2. If you do not add a constructor and initialize {} with an initialization list, the members will be initialized in the default order:

struct ceshi
{
    int frist;
    int second;
};

#define debug qDebug()<<
int main(int argc, char *argv[])
{
    ceshi c1{2,4};// frist = 2 second = 4
    ceshi c2{6};// frist = 2 second执行默认初始化 = 0
}

 

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/113815072