{"id":396,"date":"2008-06-09T08:19:28","date_gmt":"2008-06-09T06:19:28","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=396"},"modified":"2008-06-09T08:20:54","modified_gmt":"2008-06-09T06:20:54","slug":"default-struct-member-alignment-surprise-with-visual-cnet-2005","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/396","title":{"rendered":"Default Struct Member Alignment surprise with Visual C++.NET 2005"},"content":{"rendered":"<p>I am compiling C++ project which is using <a href=\"http:\/\/www.ijg.org\/\">IJG JPEG library<\/a> using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/vs2005\/default.aspx\">Visual Studio .NET 2005<\/a> (yes, 2008 is yet to come) and it came as a surprise that I stumbled upon struct data alignment issue. I am pretty sure this was not a problem with Visual Studio .NET 2003.<\/p>\n<p>The project includes my C++ files and C source from the library. C++ code includes:<\/p>\n<p><code>extern \"C\"<br \/>\n{<\/p>\n<p>#undef FAR<br \/>\n#define XMD_H\t\/\/ Otherwise INT32 is redefined<\/p>\n<p>\/\/ NOTE: See jmorecfg.h for condition compilation options<br \/>\n#include \"cdjpeg.h\"<br \/>\n#include \"jversion.h\"<\/p>\n<p>};<\/code><\/p>\n<p>Running this, I receive an error from the library (thanks for pointing this out early anyway!):<\/p>\n<blockquote><p>JPEG parameter struct mismatch: library thinks size is 360, caller expects 347.<\/p><\/blockquote>\n<p>How comes? Project settings are default and set to use default alignment <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/xh3e3fd0(VS.71).aspx\">\/Zp<\/a>. It seems as if compiler uses default value of 1 for my C++ code and uses default value of 8 for C code&#8230; with a result of break on runtime.<\/p>\n<p>The workaround is in referencing C code with alignment override using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/2e70t5y1(VS.80).aspx\">#pragma pack<\/a>:<\/p>\n<p><code>extern \"C\"<br \/>\n{<\/p>\n<p>#undef FAR<br \/>\n#define XMD_H\t\/\/ Otherwise INT32 is redefined<\/p>\n<p>\/\/#pragma pack(show)<br \/>\n#pragma pack(push, 8 )<\/p>\n<p>\/\/ NOTE: See jmorecfg.h for condition compilation options<br \/>\n#include \"cdjpeg.h\"<br \/>\n#include \"jversion.h\"<\/p>\n<p>#pragma pack(pop)<\/p>\n<p>};<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am compiling C++ project which is using IJG JPEG library using Visual Studio .NET 2005 (yes, 2008 is yet to come) and it came as a surprise that I stumbled upon struct data alignment issue. I am pretty sure this was not a problem with Visual Studio .NET 2003. The project includes my C++&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/396\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,21,20],"tags":[72,38,74,75,73],"class_list":["post-396","post","type-post","status-publish","format-standard","hentry","category-atl","category-seriously","category-wtl","tag-alignment","tag-c","tag-ijg","tag-jpeg","tag-pragma"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}