{"id":1121,"date":"2022-08-30T15:12:44","date_gmt":"2022-08-30T15:12:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/does-a-simple-cast-to-perform-a-raw-copy-of-a-variable-break-strict-aliasing-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:44","modified_gmt":"2022-08-30T15:12:44","slug":"does-a-simple-cast-to-perform-a-raw-copy-of-a-variable-break-strict-aliasing-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/does-a-simple-cast-to-perform-a-raw-copy-of-a-variable-break-strict-aliasing-collection-of-common-programming-errors\/","title":{"rendered":"Does a simple cast to perform a raw copy of a variable break strict aliasing?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve been reading about <em>strict aliasing<\/em> quite a lot lately. The C\/C++ standards say that the following code is invalid (undefined behavior to be correct), since the compiler might have the value of <code>a<\/code> cached somewhere and would not recognize that it needs to update the value when I update <code>b<\/code>;<\/p>\n<pre><code>float *a;\n...\nint *b = reinterpret_cast(a);\n*b = 1;\n<\/code><\/pre>\n<p>The standard also says that <code>char*<\/code> can alias anything, so (correct me if I&#8217;m wrong) compiler would reload all cached values whenever a write access to a <code>char*<\/code> variable is made. Thus the following code would be correct:<\/p>\n<pre><code>float *a;\n...\nchar *b = reinterpret_cast(a);\n*b = 1;\n<\/code><\/pre>\n<p>But what about the cases when pointers are not involved at all? For example, I have the following code, and GCC throws warnings about strict aliasing at me.<\/p>\n<pre><code>float a = 2.4;\nint32_t b = reinterpret_cast(a);\n<\/code><\/pre>\n<p>What I want to do is just to <em>copy<\/em> raw value of <code>a<\/code>, so strict aliasing shouldn&#8217;t apply. Is there a possible problem here, or just GCC is overly cautious about that?<\/p>\n<p><strong>EDIT<\/strong><\/p>\n<p>I know there&#8217;s a solution using <em>memcpy<\/em>, but it results in code that is much less readable, so I would like not to use that solution.<\/p>\n<p><strong>EDIT2<\/strong><\/p>\n<p><code>int32_t b = *reinterpret_cast(&amp;a);<\/code> also does not work.<\/p>\n<p><strong>SOLVED<\/strong><\/p>\n<p>This seems to be <em>a bug in GCC<\/em>.<\/p>\n<ol>\n<li>\n<p>If you want to copy some memory, you could just tell the compiler to do that:<\/p>\n<p>Edit: added a function for more readable code:<\/p>\n<pre><code>#include \nusing std::cout; using std::endl;\n#include \n\ntemplate \nT memcpy(const U&amp; source)\n{\n    T temp;\n    memcpy(&amp;temp, &amp;source, sizeof(temp));\n    return temp;\n}\n\nint main()\n{\n    float f = 4.2;\n    cout<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:30:31. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been reading about strict aliasing quite a lot lately. The C\/C++ standards say that the following code is invalid (undefined behavior to be correct), since the compiler might have the value of a cached somewhere and would not recognize that it needs to update the value when I update b; float *a; &#8230; int [&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-1121","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1121","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=1121"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}