{"id":12133,"date":"2010-12-02T07:00:00","date_gmt":"2010-12-02T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/12\/02\/the-alignment-declaration-specifier-is-in-bytes-not-bits\/"},"modified":"2010-12-02T07:00:00","modified_gmt":"2010-12-02T07:00:00","slug":"the-alignment-declaration-specifier-is-in-bytes-not-bits","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20101202-00\/?p=12133","title":{"rendered":"The alignment declaration specifier is in bytes, not bits"},"content":{"rendered":"<p>\nExplicit object\nalignment is not something most people worry about when writing code,\nwhich means that when you decide to worry about it,\nyou may be a bit rusty on how the declarations work.\n(After all, if it&#8217;s something you worried about all the time,\nthen you wouldn&#8217;t have trouble remembering how to do it!)\n<\/p>\n<p>\nI was looking at some customer code, and there was a class who\nhad a data member with an explicit alignment declaration.\n<\/p>\n<pre>\nclass Whatever {\n    ...\n    __declspec(align(32)) LONG m_lSomething; \/\/ Must be DWORD-aligned to make writes atomic\n    ...\n};\n<\/pre>\n<p>\nI pointed out that the comment didn&#8217;t match the code.\nThe comment says that the variable needs to be DWORD-aligned\n(which in Windows-speak means aligned on a 32-bit boundary),\nbut the code aligns it on a 32-<i>byte<\/i> boundary,\neight times as generous as required.\nOn the other hand, maybe they really did want the member\naligned on a 32-byte boundary (say to put it on its own cache line).\n<\/p>\n<p>\nTurns out that in this case, the comment was correct and the code was wrong.\nTo force a variable to align on a DWORD boundary, you want to say\n<code>__declspec(align(4))<\/code>.\nSave yourself a bunch of unnecessary padding bytes.\n<\/p>\n<p>\nBut in fact, in this case, the customer was simply trying too hard.\nThe code was compiled with default alignment,\nwhich aligns integer types on their natural boundaries anyway.\nThe compiler was going to align the variable\neven if you didn&#8217;t specify anything.\n<\/p>\n<p>\n[Raymond is currently away; this message was pre-recorded.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explicit object alignment is not something most people worry about when writing code, which means that when you decide to worry about it, you may be a bit rusty on how the declarations work. (After all, if it&#8217;s something you worried about all the time, then you wouldn&#8217;t have trouble remembering how to do it!) [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-12133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Explicit object alignment is not something most people worry about when writing code, which means that when you decide to worry about it, you may be a bit rusty on how the declarations work. (After all, if it&#8217;s something you worried about all the time, then you wouldn&#8217;t have trouble remembering how to do it!) [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=12133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12133\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=12133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=12133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=12133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}