c++ - How to use pack in vs and gcc -
until today used next technique pack structures:
#pragma pack(push,1) struct mystruct { char a1; char a2; int a3; } #pragma pack(pop) mystruct mydata() { mystruct ms; ms.a1='a'; ms.a2='b'; ms.a3=12; homecoming ms; }
and assumed ms packed 1, today told me in above definition, ms packed 4, since pack has no effect on definition, on declaration. http://msdn.microsoft.com/en-us/library/aa273913%28v=vs.60%29.aspx
can clarify if did right or not?
the standard specifies §3.1/2
a declaration definition unless declares function without specifying function’s body (8.4), contains extern specifier (7.1.1) or linkage-specification25 (7.5) , neither initializer nor functionbody, declares static info fellow member in class definition (9.2, 9.4), class name declaration (9.1), opaque-enum-declaration (7.2), template-parameter (14.1), parameter-declaration (8.3.5) in function declarator not declarator of function-definition, or typedef declaration (7.1.3), alias-declaration (7.1.3), using-declaration (7.3.3), static_assert-declaration (clause 7), attributedeclaration (clause 7), empty-declaration (clause 7), or using-directive (7.3.4).
thus have construction definition correctly noted, declaration case
pack has no effect on definitions
applies, not declaration. in fact msvc , gcc/clang correctly pack above 1
struct mystruct_not_packed { char a1; char a2; int a3; }; #pragma pack(push,1) struct mystruct { char a1; char a2; int a3; }; mystruct_not_packed object; // doesn't apply #pragma pack(pop) int main(int argc, char *argv[]) { std::cout << sizeof(mystruct) << std::endl; // 6 std::cout << sizeof(mystruct_not_packed) << std::endl; // 8 std::cout << sizeof(object) << std::endl; // 8 }
(tested msvc2013u4)
example gcc
c++ structure packing
No comments:
Post a Comment