{"id":271,"date":"2022-08-30T14:58:34","date_gmt":"2022-08-30T14:58:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/31\/problem-about-strict-aliasing-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:58:34","modified_gmt":"2022-08-30T14:58:34","slug":"problem-about-strict-aliasing-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/problem-about-strict-aliasing-record-and-share-programming-errors\/","title":{"rendered":"problem about strict-aliasing-Record and share programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/94e2dd813e278309b6281b4a4b7fdddd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuriousguy<br \/>\nc++ floating-point bits strict-aliasing type-punning<br \/>\nI am trying to extract the bits from a float without invoking undefined behavior. Here is my first attempt:unsigned foo(float x) {unsigned* u = (unsigned*)&amp;x;return *u; }As I understand it, this is not guaranteed to work due to strict aliasing rules, right? Does it work if a take an intermediate step with a character pointer?unsigned bar(float x) {char* c =<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/94e2dd813e278309b6281b4a4b7fdddd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuriousguy<br \/>\nc++ casting reinterpret-cast strict-aliasing type-punning<br \/>\nI&#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<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/94e2dd813e278309b6281b4a4b7fdddd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuriousguy<br \/>\nc++ c unions strict-aliasing type-punning<br \/>\nIn the comments of this answer it is said that it would be undefined behavior to split up an integer into their bytes using a union like follows. The code given at that place is similar though not identical to this, please give a note if have I changed undefined-behavior-relevant aspects of the code.union addr {uint8_t addr8[4];uint32_t addr32; };Up to now I thought this would be a fine approach to do things like addr = {127, 0, 0, 1}; and get the corr<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/94e2dd813e278309b6281b4a4b7fdddd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuriousguy<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3c0714e55ccb815144e8a072d341f4e2?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nbrofield<br \/>\noptimization gcc strict-aliasing<br \/>\nClose: The problem was the use of a pointer to a stack variable that had gone out of scope. Nothing to do with optimization. Pity that valgrind can&#8217;t find stack errors&#8230;I have a segfault that appears only when enabling -O1 level optimization in gcc 4.4.4 (CentOS 5.5). All other optimization levels (0,2,3,s) are fine. I haven&#8217;t managed to create a reduced test case for it yet, but it appears to be related to an arra<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/94e2dd813e278309b6281b4a4b7fdddd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuriousguy<br \/>\nc++ c unions strict-aliasing type-punning<br \/>\nI was working and was considering using a union. I decided against it, because the design really called for a struct\/class, but it eventually lead to the following hypothetical question:Suppose you have a union like this contrived example:typedef union {char* array_c;float* array_f;int* array_i; } my_array;. . . and then you allocate one of th<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/73c5967cf90e62b10d34b1d2d0ad3aae?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nR..<br \/>\nc arrays undefined-behavior strict-aliasing<br \/>\nOne of the examples of undefined behavior from the C standard reads (J.2):\u2014 An array subscript is out of range, even if an object is apparently accessible with thegiven subscript (as in the lvalue expression a[1][7] given the declaration inta[4][5]) (6.5.6)If the declaration is changed fro<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7f7c8d49c9b6da9cbae054059ee31a22?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nZaibis<br \/>\nc sockets gcc strict-aliasing<br \/>\nIm asking my self, can i use the BSD sockets with strict aliasing on, without getting undefined behaviour by compiling with gcc?bind(sdL<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7f7c8d49c9b6da9cbae054059ee31a22?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nZaibis<br \/>\nc c99 undefined-behavior pointer-arithmetic strict-aliasing<br \/>\nI&#8217;m working now for some weeks with c99 focusing undefined behaviour. I wanted to test some strange code while trying to respect the rules. The result was this code:(plz forgive me the variable names, i had eaten a clown)int main(int arg, char** argv) {unsigned int uiDiffOfVars;int LegalPointerCast1, LegalPointerCast2, signedIntToRespectTheRules;char StartVar;\/\/Only use to have an adress from where we can move onchar *TheAccesingPointer;int iTargetOfPointeracces;iTargetOfPointeracces= 0x55555555;TheAccesingPointer = (char *) &amp;StartVar;LegalPointerCast2 = (int) &amp;StartVar;LegalPointerCast1 = (int) &amp;iTargetOfPointeracces;if ((0x80000000 &amp; LegalPointerCast2) != (0x80000000 &amp; LegalPoi<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/6819fac41db50ea51d89e3d5fb89ad87?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nGerasimos R<br \/>\nc++ unions strict-aliasing<br \/>\nI know that for the code below, &#8220;Illegal&#8221; below is undefined (while some compilers allow it), because union member &#8220;a&#8221; is active, and then we read from union member &#8220;b&#8221;. The question is, does the code in &#8220;AmILegal&#8221; fix it, or am I doing something scary and even more obscure? Can I use memcpy to achieve the same effect or is there another undefined behaviour I am invoking there?EDIT: Maybe the examp<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/611b91012610bb163b25d02869dbf5ad?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nFramester<br \/>\nc optimization gcc pointers strict-aliasing<br \/>\nI used the following piece of code to read data from files as part of a larger program.double data_read(FILE *stream,int code) {char data[8];switch(code) {case 0x08:return (unsigned char)fgetc(stream);case 0x09:return (signed char)fgetc(stream);case 0x0b:data[1] = fgetc(stream);data[0] = fgetc(str<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4810c6c963334e5dce573e6671d746ec?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ntimrau<br \/>\nc gcc type-conversion unions strict-aliasing<br \/>\nDo you have any horror stories to tell? The GCC Manual recently added a warning regarding -fstrict-aliasing and casting a pointer through a union:[&#8230;] Taking the address, casting the resulting pointer and dereferencing the result has undefined behavior [emphasis added], even if the cast uses a union type, e.g.:union a_union {int i;double d;};int f() {double d = 3.0;return ((union a_union *)&amp;d)-&gt;i;}Does anyone have an example to illustrate this undefined behavior?Note this question is not about what the C99 standard says, or does not say. It is about the actual functioning of gcc, and other existing compilers, today.I am only guessing, but one potential problem may lie in the setting of d to 3.0. Because d is a temporary variable which is never directly read, and which is never read via a &#8216;somewhat-compatible&#8217; pointer, the compiler may not bother to se<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/94e2dd813e278309b6281b4a4b7fdddd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuriousguy<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c032a0e8e40dd79b715420e4c5d03b00?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nmeagar<br \/>\nc pointers casting c99 strict-aliasing<br \/>\nThis is the fast inverse square root implementation from Quake III Arena:float Q_rsqrt( float number ) {long i;float x2, y;const float threehalfs = 1.5F;x2 = number * 0.5F;y = number;i = * ( long * ) &amp;y; \/\/ evil<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3a1a21f034bb84c66ff9b64317bfdfea?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMankarse<br \/>\nc const language-lawyer strict-aliasing<br \/>\nDoes the following code in C have defined behavior?int main() {const int i = 0;return *(int*)(&amp;i); }I ask because 6.5\/7 lists &#8220;a qualified version of a type compatible with the effective type of the object&#8221; as a valid alias. But the effective type of the object is const int, and I don&#8217;t think int is a qualified version of const int (although the reverse is true). Neither are int a<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/928847a1c1a4201e517b311f23b89d9b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nLeonid<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-08-31 08:08:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>curiousguy c++ floating-point bits strict-aliasing type-punning I am trying to extract the bits from a float without invoking undefined behavior. Here is my first attempt:unsigned foo(float x) {unsigned* u = (unsigned*)&amp;x;return *u; }As I understand it, this is not guaranteed to work due to strict aliasing rules, right? Does it work if a take an [&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-271","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/271","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=271"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}