Correct type for array indices

S

Sean Hamilton

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.

Thanks,
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Sean said:
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.

size_t is a good choice. off_t is not part of standard C, so obviously
that is not a good choice.

Bjørn
 
C

Christian Bau

Sean Hamilton said:
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.

Use int if the number of elements is less than 32767, use long if the
number of elements is less than 2 billion.
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Christian said:
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
 
M

Malcolm

Sean Hamilton said:
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.
In ANSI C, size_t is correct, since an array may not be larger than size_t's
maximum value.
However it is rather ugly. Also, is it reasonable to suppose that you might
have a company with more than 32767 employees, and also reasonable to
suppose that you might be running on a machine with 16-bit ints, but surely
not reasonable to suppose that a company with more that 32768 employees
would runs its payroll on a machine with 16 bits.
In practise int is better. You also have the advantage that, assuming garbge
values are random, 50% of them will be negative. If indicies are ints you
therefore have a reasonable chance of detecting garbage.
 
O

Old Wolf

Christian Bau said:
Unsigned types like size_t always have the disadvantage that they behave
in non-mathematical ways when you subtract 1 from 0.

Au contraire.. it behaves according to the rules of modular
arithmetic. Much better than signed types, which can cause
undefined behaviour when you subtract them.
 
C

Christian Bau

Bjørn Augestad said:
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.

Yes, you could. Actually, you have to. Which means that of two similar
ways to write your code, one contains a subtle bug, and one doesn't.
Sometimes you will get it wrong. Much better to avoid the problem in the
first place.
Since functions/operators like sizeof/malloc/strlen/strspn and many,
many more all returns or expects a size_t, why not use it?

See reasons above. Just because the C Standard Library functions do it,
doesn't mean it is a good idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top