Re: Correct type for array indices
From: Bjørn Augestad (boa_at_metasystems.no)
Date: 11/27/04
- Next message: sam1967_at_hetnet.nl: "Re: PORTING GMP PROGRAMS TO WINDOWS"
- Previous message: Roger Leigh: "Wide character support (was: wcsftime output encoding)"
- In reply to: Christian Bau: "Re: Correct type for array indices"
- Next in thread: Christian Bau: "Re: Correct type for array indices"
- Reply: Christian Bau: "Re: Correct type for array indices"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 27 Nov 2004 12:56:46 GMT
Christian Bau wrote:
> In article <pan.2004.11.27.06.44.59.446630@bel.bc.ca>,
> Sean Hamilton <sh@bel.bc.ca> wrote:
>
>
>>Hello,
>>
>>Suppose I have a pointer to some structures. What would be the correct
>>type to store the number of entries in that array, as well as for indexing
>>that array? size_t seems like an obvious choice, but off_t also jumps to
>>mind.
>
>
> Unsigned types like size_t always have the disadvantage that they behave
> in non-mathematical ways when you subtract 1 from 0.
>
> if (i > count-1)
>
> will give a complete nonsense result if count == 0. So you have to watch
> out constantly to get everything right.
Is that really a problem? I know that the code above is just an example,
but it could easily be written as
if(i + 1 > count)
if(i >= count)
and there would be no problem, as far as I can tell.
>
> Use int if the number of elements is less than 32767, use long if the
> number of elements is less than 2 billion.
Since functions/operators like sizeof/malloc/strlen/strspn and many,
many more all returns or expects a size_t, why not use it?
Bjørn
- Next message: sam1967_at_hetnet.nl: "Re: PORTING GMP PROGRAMS TO WINDOWS"
- Previous message: Roger Leigh: "Wide character support (was: wcsftime output encoding)"
- In reply to: Christian Bau: "Re: Correct type for array indices"
- Next in thread: Christian Bau: "Re: Correct type for array indices"
- Reply: Christian Bau: "Re: Correct type for array indices"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|