This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Question on strict aliasing in C.
Andrew Haley wrote:
> On 05/07/11 14:53, Georg-Johann Lay wrote:
>> Andrew Haley wrote:
>>> On 04/07/11 19:53, Georg-Johann Lay wrote:
>>>> Paul Smith wrote:
>>>>> On Mon, 2011-07-04 at 19:17 +0200, Georg-Johann Lay wrote:
>>>>>>>> double copysign (double x, double y)
>>>>>>>> {
>>>>>>>> unsigned short * const px = (unsigned short*)(char*)&x + 3;
>>>>>>>> unsigned short * const py = (unsigned short*)(char*)&y + 3;
>>>>> I don't get why you have the (char*) cast here?
>>>> It's not a source of mine. I don't know the reason why it's there.
>>> I suspect it shuts up a compiler warning. :-)
>> No, gcc does not warn with -Wstrict-aliasing; not 4.5.2 and not 4.7
>> trunk; not with and not without (char*).
>
> That really is odd. This is exactly the kind of code we should warn about.
>
> Andrew.
For native gcc 4.3.2 [gcc-4_3-branch revision 141291] (SUSE Linux)
I see the warnings (one line with char*, one without):
copysign.c:5: warning: likely type-punning may break strict-aliasing
rules: object '*px' of main type 'short unsigned int' is referenced at
or around copysign.c:8 and may be aliased to object 'x' of main type
'double' which is referenced at or around copysign.c:9.
copysign.c:6: warning: likely type-punning may break strict-aliasing
rules: object '*py' of main type 'short unsigned int' is referenced at
or around copysign.c:8 and may be aliased to object 'y' of main type
'double' which is referenced at or around copysign.c:6.