I'm trying to swap the high/low DWORDs in a QWORD. I reference the QWORD as an array of two DWORDS and swap them, but after doing so the value of the QWORD is scrambled. Optimisations are disabled (MSVC++ 2008) and I've even tried making varToSwap and *p volatile but the output is no difference.
It looks right to me, but a union should work too (and maybe would give better code, by not requiring the variable to be moved to a pointer first, if the compiler is smart, and would avoid the risk of memory corruption on some environment where long long is not 2 ints in size).
That is quite odd. I just created a small "app" that does this with GCC just to make sure the logic was correct and the following code worked as expected:
'<<' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)"
'>>' : shift count negative or too big, undefined behavior
Then the result is also weird. I'm think I need to reinstall Visual Studio since my compiler is being very buggy. I'm getting crashes when I attempt to call 'new' or malloc. If the problem still persists I'll report the bug to MS.
You might want to use "unsigned __int64" rather than "long long" too to explicitly tell the compiler you want a 64-bit type. Just a thought, although long long should really be 64-bit
Your code violates C aliasing rules. It may work in some contexts and not in others; it's up to the compiler.
The standard states that two pointers of different types may never reference the same region of memory. For this kind of thing, either use a union or series of shifts.