[ create a new paste ] login | about

Link: http://codepad.org/eGO0Rcc4    [ raw code | output | fork | 1 comment ]

apronchenkov - C++, pasted on Aug 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm> 
#include <stdint.h> 

int main()
{
    uint32_t a = 0xffff0000;

    std::swap(
        ((uint16_t *)&a)[0],
        ((uint16_t *)&a)[1]
    );

    printf("%08X\n", a);

    return 0;
}


Output:
1
2
3
4
cc1plus: warnings being treated as errors
In function 'int main()':
Line 9: warning: dereferencing type-punned pointer will break strict-aliasing rules
Line 10: warning: dereferencing type-punned pointer will break strict-aliasing rules


Create a new paste based on this one


Comments:
posted by apronchenkov on Aug 10
One compiler produces incorrect code with O3 optimization. It is g++ (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4).

reply