r/cprogramming • u/TorontoDude12344 • Oct 23 '26
Technically speaking, are we supposed to only index arrays using size_t?
Is this technically the best way to index an array?
for (size_t i = 0; i < sizeof(a); i++)
{
a[i];
}
This makes sure that any array size, across all machines can be indexed?
22
Upvotes
27
u/zhivago Oct 23 '26
It is safe but not required.
You can use any integer with a suitable range for that particular array.
4
u/pjl1967 Oct 23 '26
In order for this to be a concern, you'd have to have a very large array, more than 2147483647 elements for a 32-bit
intindex or more than 4294967295 elements for a 32-bitunsigned int. If it were an array ofchar, it would have to be over 4GB before it mattered.So generally it simply doesn't matter.
8
u/Zirias_FreeBSD Oct 23 '26
It does exactly what you say: It's guaranteed to work for every possible array size on any target platform.
Still you're not supposed to only ever use
size_t. Instead, you are supposed to know when to use it. Which is, at least, whenever the code in question can't know the maximum size it will deal with.In a simple case with a fixed and small-enough array size, using e.g.
intis perfectly acceptable (it's guaranteed to have at least 16 bits on any target platform) and you will see it a lot in the wild.4
u/NyxTheia Oct 23 '26 edited Oct 23 '26
Friendly reminder that if `a` is of type `<type>[N]`, then `sizeof(a)` will return `N * sizeof(<type>)`. You can define a macro for actually calculating `N` in this case. But you generally shouldn't recalculate `N` in each loop and you may refactor it out to be stored before the for-loop.
If `a` is of type `<type> *`, either by definition or because it is passed as an argument to a function, you have to calculate the length with some other way and then store it to pass as one of the arguments to functions and such since `sizeof(a)` will in this case return `sizeof(<type> *)`.
2
u/gwenbeth Oct 23 '26
If you have a function that gets passed an array and a size (like strn* functions) then use size_t. But if you have an array where the size is #define ed to only 25 items, just go ahead and use an int .
2
u/flyingron Oct 23 '26
You’re allowed to use negative indices as long as the resultant address is within the original object.
1
u/aalmkainzi Oct 23 '26
ptrdiff_tis the standard type for pointer arithmetic. Butsize_tworks1
u/DawnOnTheEdge Oct 23 '26 edited Oct 23 '26
I have seen some coding standards prefer
ptrdiff_ton the grounds that a lot of bugs show up as a negative index, which is more intuitive to check for than wraparound of unsigned types. I regretfully have to disagree, since signed overflow is undefined behavior, originally to support some sixty-year-old machines that would trap on integer overflow, but which this generation of compilers takes as permission to silently insert security bugs in your code.You should definitely not use
int, since that is often too small to index many arrays, as well as being signed. Asize_tis the smallest unsigned type guaranteed to be able to index any element of any array. However, you might know for certain that a 16-bit or 32-bit value is enough to index a particular array, and want to save space. Although it’s not common practice, this would also be a good use case for something likeuint_fast16_t, to use the fastest type that’s big enough on systems wheresize_tmight be wider than the native word size.1
u/Total_Ad803 Oct 25 '26
Good practice.
1
u/trannus_aran Oct 26 '26
Nah, you're only supposed to use
charfor indices1
u/Key_River7180 Oct 27 '26
You can, and its encouraged, but it's ok to use int
1
u/arkt8 Oct 23 '26
No... you can just use any integer type but not negative.
The use of size_t (tipically an unsigned long int in a Linux amd64 architecture) ensures the value is always unsigned (you'd never have a negative index, right? right?).
Also having a common type alias to the index or length allows a predictable api on development.
If you write some library to handle arrays, it is better to use a type for indexes that reduce errors like negative values or make certain values unreachable due to smaller type on function signature.
0
u/j-joshua Oct 24 '26
Yes.