473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Correct type for array indices

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,

--
Sean Hamilton <sh@bel.bc.ca>
Nov 14 '05 #1
6 1672
Sean Hamilton 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.


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

Bjørn
Nov 14 '05 #2
In article <pa****************************@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.

Use int if the number of elements is less than 32767, use long if the
number of elements is less than 2 billion.
Nov 14 '05 #3
Christian Bau wrote:
In article <pa****************************@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

Nov 14 '05 #4

"Sean Hamilton" <sh@bel.bc.ca> wrote
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.
Nov 14 '05 #5
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:

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.
Nov 14 '05 #6
In article <iQ*******************@juliett.dax.net>,
Bjørn Augestad <bo*@metasystems.no> wrote:
Christian Bau wrote:
In article <pa****************************@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.


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.
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?


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not creating the new order, but getting the remaining...
6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
9
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so...
6
by: Alan Johnson | last post by:
For various reasons I'd like to be able to inherit from the built in types, which obviously you cannot do. To get around this, I made a template to emulate the built in types. So far I can't find...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
4
by: Patrick Sullivan | last post by:
dim s_err as stringbuilder dim xx(6) as double dim ret_flag as integer ' This is a function to call an unmanaged C-style library function. ' The lib function fills an array of 6 doubles or...
9
by: dennis.sam | last post by:
Hi, Is there away to define a multi-dimensional array with respect to the number of dimensions the array has? For example, given a user spec of "a b c d", I want to create a 4 dimensional array...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.