Size of struct containing double field

前端 未结 5 1385
抹茶落季
抹茶落季 2020-12-18 10:00

Firstly, I understand byte padding in structs. But I have still a small test contain a double field in struct and I don\'t know how to explain this :

typedef         


        
5条回答
  •  青春惊慌失措
    2020-12-18 10:31

    What is strange to you (or I'm missing something)?

    The logic is the same (the padding is according to the "biggest" primitive field in the struct (I mean - int, double, char, etc.))

    As in single, you have

    1 (sizeof(char)) + 3 (padding) + 4 (sizeof(int))
    

    it's the same in data:

    1 (sizeof(char)) + 
    7 (padding, it's sizeof(double) - sizeof(char)) + 
    8 (sizeof(double))
    

    which is 16.

提交回复
热议问题