[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-co
From: |
Peter Maydell |
Subject: |
Re: [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare |
Date: |
Mon, 4 Nov 2013 21:00:42 +0000 |
On 4 November 2013 20:38, Laszlo Ersek <address@hidden> wrote:
> On 11/04/13 21:07, Peter Maydell wrote:
>> If this warning is going to complain about entirely
>> safe and idiomatic code like
>>
>> int i;
>> static const int some_array[] = {
>> 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
>> };
>>
>> for (i = 0; i < ARRAY_SIZE(some_array); i++) {
>> ...
>> }
>
> (Entirely safe, and completely non-idiomatic: "i" should be size_t, as
> that is the type of the sizeof operator's result.)
Making simple loop iteration variables 'unsigned' is completely
non-idiomatic. It just happens that we're calculating the upper
bound here via a macro that coincidentally produces an
unsigned result. Insisting on unsigned also runs you into
problems with loops like
for (i = ARRAY_SIZE(foo) - 1; i >=0 ; i--) {
something(foo[i]);
}
(which obviously you can avoid by contorting the code,
but why do that when leaving 'i' as signed is much clearer?)
-- PMM