array index type

S

Szabolcs Nagy

what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}
 
M

Mark Bluemel

Szabolcs said:
what type can/should be used as array index?

OK - head for the bunkers. I can see holy war coming...
eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}


I'll only try to answer the "can" part of your question...

As I read it, the C language specification states only that the
index/subscript must be of integer type - size of the type, presence or
absence of sign is not specified.

On that basis, if your application calls for unsigned long as
index/subscript, then go ahead and use it.

I'm sure someone will be along shortly to tell you what type should be
used....
 
T

Tim Prince

Szabolcs said:
what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}

While unsigned types should work, and are probably required to do so by
the C standard, they may incur significantly more overhead than unsigned
types. unsigned long probably incurs more portability issues than other
possibilities.
 
S

Szabolcs Nagy

Tim said:
While unsigned types should work, and are probably required to do so by
the C standard, they may incur significantly more overhead than unsigned
types.

signed or unsigned?
why is there overhead?

thanks for the answer
 
S

santosh

Szabolcs said:
what type can/should be used as array index?

Any integer type is permitted by the C Standard. However in most
situations negative index values might not be needed, so an unsigned
type like unsigned or size_t should be fine.

If you anticipate the need for negative index values use a signed type
like int or long.

Which type is the "best" would depend on your implementation, (including
the characteristics of the underlying architecture), and the context of
use in your program. In one place int might be sufficient while in
another place you might need an unsigned long.

As far as C Standard goes, only the type size_t is guaranteed to be able
to store the size of the largest possible object in a program. It
defined as an unsigned integral type but the exact type is
implementation defined. In most cases it is unsigned or unsigned long.

<snip>
 
R

Richard

Tim Prince said:
Szabolcs said:
what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}

While unsigned types should work, and are probably required to do so
by the C standard, they may incur significantly more overhead than
unsigned types. unsigned long probably incurs more portability issues
than other possibilities.


Unsigned types "should work"? What on earth do you mean? I have never,
ever used anything other than unsigned types for indexing into arrays.

As for the "performance overhead" this is again something completely new
to me.
 
S

santosh

Richard said:
Tim Prince said:
Szabolcs said:
what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}

While unsigned types should work, and are probably required to do so
by the C standard, they may incur significantly more overhead than
unsigned types. unsigned long probably incurs more portability issues
than other possibilities.


Unsigned types "should work"? What on earth do you mean? I have never,
ever used anything other than unsigned types for indexing into arrays.

As for the "performance overhead" this is again something completely
new to me.


He probably means that some implementations might implement overflow
detection for signed types.
 
R

Richard

santosh said:
Richard said:
Tim Prince said:
Szabolcs Nagy wrote:
what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}

While unsigned types should work, and are probably required to do so
by the C standard, they may incur significantly more overhead than
unsigned types. unsigned long probably incurs more portability issues
than other possibilities.


Unsigned types "should work"? What on earth do you mean? I have never,
ever used anything other than unsigned types for indexing into arrays.

As for the "performance overhead" this is again something completely
new to me.


He probably means that some implementations might implement overflow
detection for signed types.


What C implementations implement overflow "detection" and what do they
do if they detect such a condition? Never saw it myself. Some obscure
embedded systems I assume? Sounds more like ADA than C.
 
V

vipvipvipvipvip.ru

You should use size_t.
The sizeof(object) cannot be > SIZE_MAX, therefore size_t is the best.
 
U

user923005

what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;



}- Hide quoted text -

- Show quoted text -


A size_t is guaranteed to work for any object you can create. You
have no such guarantee with any other integral types. If you know in
advance what the size is, then any integral type that will contain up
to that value is probably OK (but either signed or unsigned char
should be thoroughly discouraged unless you have a ridiculously good
reason for using it).

So I say:
General purpose object offset type is size_t.
Known range is whatever you know will work, but avoid char types
unless there is no other choice.

IMO-YMMV.
 
S

SM Ryan

# what type can/should be used as array index?
#
# eg. can i use unsigned long? or should it be size_t?

Any integer, including enumerations. You can also use negative
subscripts.
char *a = "abcde";
char *b = a+3;
b[-1] is then 'c'

You cannot safely index outside of allocated block or declared
array, but within that, you can safely index up and dowm.
 
M

Malcolm McLean

Mark Bluemel said:
OK - head for the bunkers. I can see holy war coming...
Yes. See what ANSI have done to our nice language. And all to neaten up the
interface to malloc().
 
P

pete

Szabolcs said:
what type can/

Any integer type can be used as array index.
should be used as array index?

It depends on two things.
1 The range of the index
2 What else you're going to do with the variable.
eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}


Either type would be fine for index_t there.

Just about the only time
that I wouldn't use size_t for an array index type,
would be if I also wanted to printf the index value in C89.
Then I might use either unsigned or long unsigned.
 
P

pete

Richard said:
Tim Prince said:
Szabolcs said:
what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}

While unsigned types should work, and are probably required to do so
by the C standard, they may incur significantly more overhead than
unsigned types.
unsigned long probably incurs more portability issues
than other possibilities.


Unsigned types "should work"? What on earth do you mean? I have never,
ever used anything other than unsigned types for indexing into arrays.

As for the "performance overhead"
this is again something completely new to me.


I've seen Tim Prince write about it before,
but I've never seen anyone else acknowledge the phenomenon.
 
T

Thad Smith

user923005 said:
A size_t is guaranteed to work for any object you can create. You
have no such guarantee with any other integral types. If you know in
advance what the size is, then any integral type that will contain up
to that value is probably OK (but either signed or unsigned char
should be thoroughly discouraged unless you have a ridiculously good
reason for using it).

Probably OK? Absolutely guaranteed.
So I say:
General purpose object offset type is size_t.
Known range is whatever you know will work, but avoid char types
unless there is no other choice.

I code for 8-bit embedded systems. Using an unsigned char for an index
is more efficient on those targets that an unsigned short/int/long/
size_t. I do have a choice and for that platform if I know there are
less than 256 elements, I choose unsigned char. It is conforming, it
works, it is efficient.
 
C

CBFalconer

Szabolcs said:
what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a;
return s;
}


This depends entirely on the declaration of the array. C arrays
have fixed length (apart from VLAs, which we will ignore). The
storage is created by the array declaration, or possibly by a
malloc returning a pointer to the array. In either case you know
how big the array is. Any indexes should be of a type that can
cover that size.
 
J

James Kuyper

santosh said:
He probably means that some implementations might implement overflow
detection for signed types.

That might impose a performance overhead on signed integers, not on
unsigned ones.
 
S

santosh

James said:
That might impose a performance overhead on signed integers, not on
unsigned ones.

I know. It's not clear whether Tim meant signed or unsigned when he was
talking about incurring overhead.
 
K

Keith Thompson

pete said:
Just about the only time
that I wouldn't use size_t for an array index type,
would be if I also wanted to printf the index value in C89.
Then I might use either unsigned or long unsigned.

Printing a size_t value in C89/C90 isn't difficult, certainly (IMHO)
not difficult enough to justify avoiding using the type.

size_t size;
/* ... */
printf("size = %lu\n", (unsigned long)size);

This isn't completely guaranteed to work in C99, but it can only fail
if the value if size exceeds ULONG_MAX (something that can't happen in
C90).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top