Add G_ARRAY_SIZE that is G_N_ELEMENTS casted to a typed size
Submitted by Behdad Esfahbod
Assigned to Behdad Esfahbod
Link to original bug (#336137)
Description
G_N_ELEMENTS currently produces an unsigned integer. It causes lots of annoying "comparison between signed and unsigned" warnings when used in loops with a typical gint as the loop variable.
In C, when signed and unsigned integers are compared togehter, the signed one is converted to unsigned before the comparison. If the value being converted is known at compile-time and a non-negative integer (like the G_N_ELEMENTS result is), no warning is generated (as no problem occurs), but if the converted value is a variable, a warning is generated, and a real problem may occur.
So, by casting G_N_ELEMENTS results to signed, we have removed the existing such warnings, without introducing new ones.
In other words, the following program only generates one warning, on the third case:
#include <stdio.h>
int main (void) { int s; unsigned int u;
scanf ("%d %u", &s, &u);
if (s <= 3) return 1;
if (u <= 3) return 2; /* NO warning here */
if (s <= 3u) return 3; /* warning here */
if (u <= 3u) return 4;
return 0;
}