Re: [hatari-devel] warning with ARRAYSIZE macro

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


Le 07/09/2016 à 23:18, Eero Tamminen a écrit :
Hi,

On 09/07/2016 11:40 PM, Nicolas Pomarède wrote:
when compiling with mingw for windows, nearly all uses of ARRAYSIZE
generate a warning :


src/gemdos.c:3200:23: warning: comparison between signed and unsigned
integer expressions [-Wsign-compare]
  for (used = i = 0; i < ARRAYSIZE(ForcedHandles); i++)

we have this for ARRAYSIZE :

#ifndef ARRAYSIZE
#define ARRAYSIZE(x) (int)(sizeof(x)/sizeof(x[0]))
  -----------------------^

Signed.

#endif

As sizeof returns a >= 0 value, we should cast to "unsigned int", or
even not cast at all, but cast the value on the left side of the
comparison to "unsigned int" instead ("i" in that case).

There's no warning with GCC/Clang, but under mingw, I guess ARRAYSIZE is
already defined with a different cast.

I assume some mingw header has ARRAYSIZE declared without (int) cast.

Remove ifndef from around the define in our header so that you
get warning telling where where mingw defines ARRAYSIZE (and
how it defines it).

Then make sure that this place contains ifndef, and if it does,
just move our definition to be included before mingw one.


Should we cast everything to unsigned to avoid warning for all
compilers ?

We need first to know why exactly it works differently with mingw.
Otherwise changing things would just get warnings with other compiler.


From what I see, mingw defined ARRAYSIZE as
(sizeof(a)/sizeof((a)[0]))  (ie no cast)
A quick googling seems to show that this is usually the case to not put a cast.

sizeof returns size_t, so should we cast both side of each comparison to (unsigned int) to handle all cases (or cast only one side if we declare the "i" variable as unsigned as it is usually used ?)






Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/