Filesizes... an int ?

Tony van der Hoff tony at mk-net.demon.co.uk
Sat Nov 2 11:53:05 GMT 2002


On 31 Oct 2002, in message <42a8388e4b%Marco at dionin.esw-heim.tu-clausthal.de>,
Marco Baye <Marco.Baye at tu-clausthal.de> wrote:

> John Tytgat <John.Tytgat at aaug.net> wrote:
> 
> > I'm using xosfile_read_no_path() and see that the file size returned
> > is an int.  Wouldn't an unsigned int not more appropriate ? Or even
> > a proper typedef for it ?
> 
> And while we're at that: os_read_monotonic_time() returns something
> called os_t, which is typedef'd to be int. Wouldn't "unsigned int"
> be more appropriate?
[snip]

Both of these are almost worthy of inclusion in the FAQ - maybe I'll do that,
but I don't suppose anyone reads it...

The rule of thumb used throughout OSLib is that anything upon which
arithmetic is likely to be performed is typed as a signed int, which makes
it possible to do comparisons. I acknowledge that using an unsigned int gives
an extra bit of information in the absence of a long long int, but at the
expense of usability; i.e. it's a bodge.

I'm sure that this is one of those subjects that can be debated 'till the
cows come home, and it is impossible to please everyone, but I rather like
the present arrangement.

The following article (from this list) was the last time it was
raised, and illustrates why I can't really change things...

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

On 19 Jul 2001, in message <9e6979c4a%alex at thouky.co.uk>, Alexander
Thoukydides <alex at thouky.co.uk> wrote:

> On 19 Jul, Tony van der Hoff <tony at mk-net.demon.co.uk> wrote:
> > I believe os_t, the monotonic time, should be unsigned, not a signed int
> > as at present.
> > 
> > OSLib practice is that anything on which arithmetic can be performed
> > should be signed, but I think that in this case the arithmetic is
> > generally to compare it with a previous time, and this is made a lot
> > harder by it being signed. I always seem to convert it to unsigned
> > anyway.
> 
> On the contrary, having it signed makes such comparisons easier. Remember,
> you have to cope with the time wrapping around... assuming that something
> else doesn't crash in the intervening sixteen months.
> 
> An explicit cast to a signed value would otherwise be required for code
> such as the following (based on the BASIC sample on PRM 3-185):
> 
>     os_t oldtime;
>     
>     /* ... */
> 
>     os_t newtime = os_read_monotonic_time();
>     while ((newtime - oldtime) > 0)
>         oldtime = oldtime + 100;
> 
> With os_t as an unsigned value the comparison would always be greater than
> or equal to zero.
> 
> > If I change it, it will be binary compatible with old code, although
> > anyone re-linking would have to recompile. 
> 
> Unfortunately, that recompilation would change the behaviour of code such
> as that shown above. Comparisons such as that are common in my code, and I
> expect they are in other people's code also.

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

-- 
Tony van der Hoff         | MailTo:tony at mk-net.demon.co.uk
                          | MailTo:avanderhoff at iee.org
Buckinghamshire, England  | http:www.mk-net.demon.co.uk



More information about the oslib-user mailing list