{"id":1060,"date":"2022-08-30T15:11:43","date_gmt":"2022-08-30T15:11:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/do-i-really-have-to-worry-about-alignment-when-using-placement-new-operator-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:43","modified_gmt":"2022-08-30T15:11:43","slug":"do-i-really-have-to-worry-about-alignment-when-using-placement-new-operator-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/do-i-really-have-to-worry-about-alignment-when-using-placement-new-operator-collection-of-common-programming-errors\/","title":{"rendered":"Do I really have to worry about alignment when using placement new operator?-Collection of common programming errors"},"content":{"rendered":"<p>When you call placement new on a buffer:<\/p>\n<pre><code>A *a = new (buf) A;\n<\/code><\/pre>\n<p>you are invoking the built-in <code>void* operator new (std::size_t size, void* ptr) noexcept<\/code> as defined in:<\/p>\n<blockquote>\n<p>c++11<\/p>\n<h3>18.6.1.3 Placement forms [new.delete.placement]<\/h3>\n<p>These functions are reserved, a C++ program may not de\ufb01ne functions that displace the versions in the Standard C++ library (17.6.4). The provisions of (3.7.4) do not apply to these reserved placement forms of operator new and operator delete.<\/p>\n<p><code>void* operator new(std::size_t size, void* ptr) noexcept;<\/code><\/p>\n<p>\nReturns: <code>ptr<\/code>.<br \/>\nRemarks: Intentionally performs no other action.<\/p><\/blockquote>\n<p>The <em>provisions of (3.7.4)<\/em> include that the returned pointer should be suitably aligned, so it&#8217;s fine for <code>void* operator new (std::size_t size, void* ptr) noexcept<\/code> to return a nonaligned pointer if one is passed in. This doesn&#8217;t let <em>you<\/em> off the hook, though:<\/p>\n<blockquote>\n<p>[14] Note: when the allocation function returns a value other than null, it must be a pointer to a block of storage in which space for the object has been reserved. The block of storage is assumed to be appropriately aligned and of the requested size.<\/p>\n<\/blockquote>\n<p>So if you pass unaligned storage to a placement-new expression you&#8217;re violating the assumption that the storage is aligned, and the result is UB.<\/p>\n<p>Indeed, in your program above, if you replace <code>long long b<\/code> with <code>__m128 b<\/code> (after <code>#include<\/code> ) then the program will segfault, as expected.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:20:38. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>When you call placement new on a buffer: A *a = new (buf) A; you are invoking the built-in void* operator new (std::size_t size, void* ptr) noexcept as defined in: c++11 18.6.1.3 Placement forms [new.delete.placement] These functions are reserved, a C++ program may not de\ufb01ne functions that displace the versions in the Standard C++ library [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1060"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1060\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}