tag:blogger.com,1999:blog-2543858175797658864.post5604210844611461891..comments2023-06-03T09:48:30.964-04:00Comments on Trash Can of Code: Using c++ references for nicer codecottonvibeshttp://www.blogger.com/profile/15542648017139504620noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-2543858175797658864.post-15463581298664629162010-07-21T19:21:12.959-04:002010-07-21T19:21:12.959-04:00BTW, I found a msdn article where they also break ...BTW, I found a msdn article where they also break strict-aliasing rules in a msvc example:<br />http://msdn.microsoft.com/en-us/library/w22adx1s.aspx<br /><br />I guess this further-confirms that it should be safe on msvc++.cottonvibeshttps://www.blogger.com/profile/15542648017139504620noreply@blogger.comtag:blogger.com,1999:blog-2543858175797658864.post-53479450306376845042010-07-12T02:25:05.148-04:002010-07-12T02:25:05.148-04:00Dark Sylinc:
You're right it does break strict...Dark Sylinc:<br />You're right it does break strict aliasing rules; and yes I'm familiar with the union-way.<br /><br />If you are to rely on this code you should have strict aliasing optimizations off if your compiler has such an option; From what I've read GCC seems to rely on the strict aliasing, whereas afaik msvc++ doesn't make such bold assumptions.<br />At least I have not had a problem with msvc++ using such techniques, and I am not aware of an option to turn off such optimizations in msvc++, so I will assume it notices when code is breaking strict aliasing (in the above example it should be obvious to the compiler).<br /><br />Also, I work with a codebase that constantly breaks strict aliasing rules all over the place; if msvc++ had a big problem with it, our project would never work.cottonvibeshttps://www.blogger.com/profile/15542648017139504620noreply@blogger.comtag:blogger.com,1999:blog-2543858175797658864.post-82024332258282464442010-07-11T14:24:25.094-04:002010-07-11T14:24:25.094-04:00I've kept thinking about it, and after more re...I've kept thinking about it, and after more research; the "union" method is also non-standard. But the "union" method apparently works better with compilers with optimizations on (even strict aliasing) because it's easier for the compiler to identify.<br /><br />Well the moral of the story: watch out for strict aliasing rules in your compiler.<br />Google "type punning" for more information.<br /><br />Hey at least you've found another way, didn't you?<br /><br />Cheers<br />Dark SylincMatias N. Goldberghttps://www.blogger.com/profile/02186368235104859313noreply@blogger.comtag:blogger.com,1999:blog-2543858175797658864.post-15902017140706636662010-07-11T14:09:10.205-04:002010-07-11T14:09:10.205-04:00Hi! I was just passing by and saw this post.
I...Hi! I was just passing by and saw this post.<br /><br />I'm sorry, but what you're doing (both methods) is undefined behavior.<br />It will _often_ work, but if you turn strict aliasing optimizations on, it may break and crash.<br /><br />The best method is to use a "union" to do this:<br /><br />union Foo<br />{<br /> int i;<br /> float f;<br />};<br /><br />int main()<br />{<br /> Foo myFoo;<br /><br /> myFoo.i = 0x7f800000;<br /> cout << myFoo.f << endl;<br /><br /> return 0;<br />}<br /><br />It's not an ugly way to it, works in C too, and will not occasionally break<br /><br />Cheers<br />Dark SylincMatias N. Goldberghttps://www.blogger.com/profile/02186368235104859313noreply@blogger.com