sm00thslp / hedgewars

Automatically exported from code.google.com/p/hedgewars
GNU General Public License v2.0
0 stars 0 forks source link

Use signed integers instead of unsigned integers (with few exceptions) #866

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Change all variables that are unsigned integers (except for flags/masks and 
internal hwFloat values) to signed.

The reason for that is that doing subtractions involving unsigned integers - 
e.g. for a comparison - can lead to unexpected behavior whenever the programmer
- does not know that a variable is of unsigned type
- forgets to think about effects of underflows
- isn't really aware of when compilers actually do casts and when they don't

e.g.: 

----------------------------

program test;

uses sysutils; // for IntToStr()

// imagine these were globals defined in an included file, rather than visibly 
here
var
    foo: LongWord;
    bar: LongInt;

begin

foo:= 0;
bar:= 0;

if Pred(foo) > bar then
    WriteLn('Pred(' + IntToStr(foo) + ') is greater than ' + IntToStr(bar) + '.')
else
    WriteLn('Pred(' + IntToStr(foo) + ') is lesser than '  + IntToStr(bar) + ' (or equal).');

end.

----------------------------

and

----------------------------

#include <inttypes.h>
#include <stdio.h>

// imagine these were globals defined in an included file, rather than visibly 
here
uint32_t foo;
 int32_t bar;

int main(int argc, char** argv)
{
    foo = 0;
    bar = 0;

    if (foo - 1 > bar)
        printf("%d - 1 is greater than %d.\n", foo, bar);
    else
        printf("%d - 1 is lesser than %d (or equal).\n", foo, bar);

    return 0;
}

----------------------------

will output
    Pred(0) is greater than 0.
and
    0 - 1 is greater than 0.
respectively.

Original issue reported on code.google.com by sheepyluva on 15 Dec 2014 at 6:05