
Václav Haisman wrote:
Some time ago, I posted a link here to my (then new) white paper on strict-aliasing. I wrote it because people keep posting, on this and on other lists, questions that show a basic misunderstanding of aliasing and what the rules in C and C++ standards on aliasing are intended to communicate. I got some great feedback and have posted a new revision, http://dbp-consulting.com/StrictAliasing.pdf. I'm hoping that people on this list will review the paper and tell me some more ways of improving it. My intention is to write a paper that will make it so that I won't have to keep answering the same questions over and over again;) I'll just point people at the paper. Nice paper. But I believe parts are wrong or misleading. Misleading is the
Patrick Horgan wrote, On 3.1.2011 7:11: part about -fno-strict-aliasing. It is GCC specific. The wrong part, I think, is the part suggesting union as a solution. As far as I know you can only read from union through a member that you have put into it. The fact that you can access different union member is also an extension, though one more common than just GCC specific.
I second that. FWIW, i tried the memcpy version*. Surprise: It leads to the exact same (assembly) code. I guess you should outline that, and not rely on that extension. *: uint32_t swaphalves(uint32_t a) { uint16_t as16bit[2]; memcpy(as16bit, &a, sizeof(a)); uint16_t tmp; tmp = as16bit[0]; as16bit[0] = as16bit[1]; as16bit[1] = tmp; memcpy(&a, as16bit, sizeof(a)); return a; }