???????C/C++??????
???????????? ???????[ 2015/11/19 10:48:59 ] ????????.NET ???????????
???????????memory alignment.?????????????????????????????????t?????????????????????????????????δ???????棬????????????????????????????????????????????η????
???????????????cpu access memory??Ч??(??????????)??????(???Щ?????£??????ж??????????????????).????cpu???????????????.?Щcpu?????(??仰??????????????????cpu????)??????Щ??????????????????????????????????????.???????????????????????????.
???????????2????????????????????????????????????.???????Щ??????????????????????.???c/c++???????????????.???????????????е????????????.
?????????仰??:??????????????????.
????1.???:????????????.
????1???????(??????)?????????е?????????????????????????????????Щ???????????Щ???????Щ???????????????????????????
????2??????????????(???????)??t?????????????????????????????????δ???????棬?????????????????????????????????????????η????
?????.???????????????????
????????????????????????byte?????????????????????κ???????????????????κε??????????????????????????????????????????? ???????????????????????????????????????????????????У????????????????????????????????
????????????ú???????????????洢??????????к????????Щ?????Щ?????????????????Щ????????????????????Щ?????CPU????? ?????н??ж???????????????????????????????±????????????.????????????????????????????????????????????????????? ????????ж?????????Ч??????????????????Щ????ζ?????????????????????int????????32λ??????????????????????????? ??????????????????32bit????????????????????????????2????????????????ζ???????????????????????????32bit?? ??????????Ч??????????
??????.??????????????:
??????????????????????(32bit??x86??????gcc??????):
???????????????壺
????struct A
????{
????int a;
????char b;
????short c;
????};
????struct B
????{
????char b;
????int a;
????short c;
????};
???????????32λ?????????????????????????:
????char:1(?з?????????)
????short:2(?з?????????)
????int:4(?з?????????)
????long:4(?з?????????)
????float:4 double:8
???????????????????С??????
?????????:
????sizeof(strcut A)??8
????sizeof(struct B)??????12
????????A?а?????4???????int?????1???????char?????2???????short???????????B????;?????A??B??С??????7????
???????????????????????????????????????????????ж?????????????????????????y??ж????????????????????????????????????????????????????????.????:
????#pragma pack (2) /*?????2??????*/
????struct C
????{
????char b;
????int a;
????short c;
????};
????#pragma pack () /*???????????????????*/
????sizeof(struct C)???8??
????????????1??
????#pragma pack (1) /*?????1??????*/
????struct D
????{
????char b;
????int a;
????short c;
????};
????#pragma pack () /*???????????????????*/
????sizeof(struct D)??7??
?????????????????#pragma pack()??????.
??????.??????????????????????ж?????
????????????????????????????
????1.???????????????????
????????char??????????????????1??????short???2??????int??float??double????????????????4????λ????
????2.???????????????????????????????????????????
????3.??????????#pragma pack (value)???????????value??
????4.??????????????????Ч???????????????????????????С????????
?????? ????Щ??????????????????????????????????????????????????Ч?????N???????????????????????????????????Ч????N???? ???“??????N??”?????????????"?????????%N=0".????????е??????????????????????????????????????????????????????? ????????????????????????????????????屾???????????????Ч????????(??????????????????????????????Ч??????????? ??????????????????)??????????????????????????????
?????????????
????????????B??
????struct B
????{
????char b;
????int a;
????short c;
????};
?????? ??B???????0x0000?????????????????ж??????????????????????£????????4??????????????b????????????1????????????????? ?????4С??????????Ч??????1????????????0x0000????0x0000%1=0.????????????a?????????????4????????Ч???????4?? ??????????????????0x0004??0x0007?????????????????У?????0x0004%4=0???????????????????????????c??????????? 2????????Ч????????2??????????0x0008??0x0009????????????У?????0x0008%2=0???????0x0000??0x0009???? ????B?????????????B?????????????????д?????(??????b????????4????????????Ч????????4????????????????? 0x0009??0x0000=10??????10??2????4??0??????0x0000A??0x000B??????B????á???B??0x0000??0x000B ????12??????sizeof(struct B)=12;???????????????????????????????? ???????????????0???????????????????????油??2???????????????????????????????Ч????????????????????????B?????飬?? ????????????????0???????????????????????????????壬????????????????????????????????????С?????4???????????????? ????????????????0x0000A?????????????????????????????????????????????Ч?????С????????.???????:????char????????? ?????????1??????short???2??????int??float??double????????????????4????Щ???????????????????????????????? ???????Щ?????????????????????????????????????.
????????????????????C??
????#pragma pack (2) /*?????2??????*/
????struct C
????{
????char b;
????int a;
????short c;
????};
????#pragma pack () /*???????????????????*/
?????????????b???????????1???????????2???????????Ч??????1??????C??0x0000????????b?????0x0000??????0x0000%1= 0;????????????????????4???????????2????????Ч??????2?????????????0x0002??0x0003??0x0004??0x0005??????? ????У?????0x0002%2=0????????????c???????????2????????Ч??????2???????
??????0x0006??0x0007?У????? 0x0006%2=0???????0x0000??0x00007????????????C?????????C???????????4??????C????Ч??????2????8%2=0??C ????0x0000??0x0007????????????sizeof(struct C)=8.
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11