C/C++?????????????
?????Moondark ???????[ 2016/12/30 11:17:47 ] ??????????????????? C++
?????????sizeof
?????????????壺
????struct S1
????{
????char c;
????int i;
????};
????sizeof(s1)??VC6?а???????????????8??
?????????????sizeof?????——sizeof??????????????????????????????????e????????????????S1???????????
????S1 s1 = { 'a'?? 0xFFFFFFFF };
???????????????????????????г??????s1???????棬????????
?????????VC6.0(sp6)?????s1?????0x0012FF78???????????????£?
????0012FF78: 61 CC CC CC FF FF FF FF
????????????????м??????3??????CC????MSDN????????
????When applied to a structure type or variable?? sizeof returns the actual size?? which may include padding bytes inserted for alignment.
??????????????????е??????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????д???????????????????????????????????????2??????????????short?????λ?????2????????????????4??????????????int?????λ?????4????????????????????????????????м???????????????????????????????sizeof????????
????????????????S1??char??int??λ???
????struct S2
????{
????int i;
????char c;
????};
????????sizeof(S2)?????????????????8???????棬??????c?????????3????????????????????????????????????ɡ?
???????????????????????????????????????????????
????1) ?????????????????????????????????С????????
????2) ???????????????????????????????offset??????????С??????????????????????????????????????????internal adding????
????3) ????????С?????????????????С???????????????????????????????????????????????trailing padding????
?????????????????м???????????
????1) ??治??????????????????С???????????????????????????????????1?????????????????????????????????????????????????????????
??????????????????????????????????????????offsetof()??????????????stddef.h?ж??壬???£?
????#define offsetof(s??m) (size_t)&(((s *)0)->m)
???????磬??????S2??c??????????????size_t pos = offsetof(S2?? c);// pos????4
????2) ????????????????????char??short??int??float??double????????????????????????????“??????”?????sizeof???С????????????????????????????????????????壬???????????????????????????????????????????????????????????????????????塣??????????????????????λ??????????????????????忴?????????????????е??????????????е????????????????????????????????????VC6?????????????????
????struct S3
????{
????char c1;
????S1 s;
????char c2
????};
????S1??????????????int??S3?????????????????S1“???”?????????S3?????????int???????????S3????????????洢????????????4??????????sizeof(S3)????????4??????c1????????0??s????????????s????????壬??????????????????????????????????С?8????????4??c1??s???????3???????????c2??s?????????????c2????????12??????c2???С?13??13??????4????????????β???ò???3?????????????sizeof(S3)????16??
???????????????????????????????????
??????????С?????????????????????????С??????β?????????????????
????sizeof( struct ) = offsetof( last item ) + sizeof( last item ) + sizeof( trailing padding )
????????????????????????sizeof?????????μ??????????????????磬????????sizeof???????????δ??????????????????pack????????????????????????????????????????÷????в????VC6?????#pragma pack???????????????/Zp???????#pragma pack??????÷????#pragma pack( n )??n????????????????1??2??4??8??16???????8??????????????????sizeof?С??????ó?????????????????????????????????????????????????С?????????£?
????offsetof( item ) = min( n?? sizeof( item ) )
????????????
????#pragma pack(push) // ?????pack??????????
????#pragma pack(2)// ????????嶨???????
????struct S1
????{
????char c;
????int i;
????};
????struct S3
????{
????char c1;
????S1 s;
????char c2
????};
????#pragma pack(pop) // ????????pack????
????????sizeof(S1)???min(2?? sizeof(i))????2??????i????????2??????sizeof(i)????6???????2??????????????S1???С?6???????????sizeof(S3)??s????????2??c2????????8??????sizeof(c2)????9???????2???????????????????????sizeof(S3)????10??
????????????????????????????????:)
???????????????“?????”?????????????????С???0??????1?????????“??????”???????α????????????????“?????”????????ε??????????????“?????”?????????洢????????????????????????????????????λ??????£?
????struct S5 { };
????sizeof( S5 ); // ????1
??????λ??????sizeof:
???????????????λ??????????????sizeof??????????????????????λ???????sizeof??????????????????????????????????
????C99?漲int??unsigned int??bool???????λ??????????????????????????????????????????????????????
???????λ???????????????洢??????1??????
????1) ???????λ????ε??????????????λ?????С???????sizeof??С??????????ν???????????δ洢?????????????????
????2) ???????λ????ε??????????????λ?????????????sizeof??С??????????ν????μ?洢???????????????????????С??????????
????3) ????????λ????ε???????????????????????????в???VC6?????????????Dev-C++???????????
????4) ???λ?????????????λ????Σ????????????
????5) ????????????С??????????????С??????????
?????????????????????????
???????1??
????struct BF1
????{
????char f1 : 3;
????char f2 : 4;
????char f3 : 5;
????};
????????沼?????
????|__f1___|____f2___ |__|____f3______|______|
????|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
????λ???????char????1??????????????f1??f2??????f2?????????1??????У???f3???????????????????sizeof(BF1)?????2??
???????2??
????struct BF2
????{
????char f1 : 3;
????short f2 : 4;
????char f3 : 5;
????};
????????????λ????????????VC6????sizeof?6????Dev-C++???2??
???????3??
????struct BF3
????{
????char f1 : 3;
????char f2;
????char f3 : 5;
????};
??????λ????δ????????У???????????????VC6??Dev-C++?е?????С???3??
??????
???·???
??????????????????
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