Fix warnings in libobjc

Nicola Pero nicola@brainstorm.co.uk
Tue Jun 18 11:11:00 GMT 2002


>  > From: Nicola Pero <nicola@brainstorm.co.uk>
>  > 
>  > swap.m:
>  > =======
>  > #include <stdio.h>
>  > 
>  > int main()
>  > {
>  >   union swap 
>  >     {
>  >       unsigned short num;
>  >       unsigned char  byt[2];
>  >     } dst;
>  >   unsigned short i, j;
>  >   union swap *src; 
>  >   
>  >   i = 0x456;
>  > 
>  >   src = (union swap*)&i;
>  >   dst.byt[0] = src->byt[0];
>  >   dst.byt[1] = src->byt[1];
>  >   j = dst.num;
>  >   
>  >   if (j != 0x456)
>  >     {
>  >       printf ("Test error: j == %x\n", j);
>  >       abort ();
>  >     }
>  >   return 0;
>  > }
> 
> I think this test only works if sizeof(short) == 2.

Yes - my mistake - I actually knew that :-) when I first wrote the
testcase, I forgot about it


> That's true on most cpus in gcc, but not all IIRC.
> 
> Perhaps if you change the size of `byt' to be:
> 
>  >   union swap
>  >     {
>  >       unsigned short num;
>  >       unsigned char  byt[sizeof(unsigned short)];
>  >     } dst;
> 
> and then loop over sizeof(src->byt) for assigning dst it'll be ok for
> all cases.  E.g.:
> 
>  >   unsigned short x;
>  >   src = (union swap*)&i;
>  >   for (x=0; x < (sizeof(src->byt)); x++)
>  >     dst.byt[x] = src->byt[x];
>  >   j = dst.num;

Thanks - for me this change would be ok 



More information about the Gcc-patches mailing list