內存對齊

雖然內存對齊是老話題,但仍是時常犯錯,幾下個例子,謹記:spa

 1 void func10()
 2 {
 3     //要牢記,默認按4字節對齊,其餘就很容易想了===============================
 4     union u_my
 5     {
 6         int npara;
 7         char name[6];
 8     };
 9 
10     int sizeunion = sizeof(u_my);  //8
11 
12     struct st_my
13     {
14         int nprice;
15         short spara;
16         u_my  uobj;
17     };
18 
19     int nsize = sizeof(st_my);  //16
20 }

其餘規則,參考這個例子就清楚了。code

相關文章
相關標籤/搜索