Shao Miller <> writes:
> On 1/13/2013 19:57, Keith Thompson wrote:
>> Shao Miller <> writes:
>>> On 1/13/2013 18:39, Keith Thompson wrote:
>> [...]
>>>> So you'd rather iterate over every element of the array, and then
>>>> test i%10 to decide whether to process the current one?
>>>>
>>>> for (i = 0; i < sizeof arr / sizeof arr[0]; i ++) {
>>>> if (i % 10 == 0) {
>>>> /* ... */
>>>> }
>>>> }
>>>>
>>>> I find that more verbose, less clear, and almost certainly less
>>>> efficient (unless the compiler is clever enough to transform it
>>>> into the first form). All to solve a problem that is not a problem
>>>> at all.
>>>>
>>>> If there's some concern about i overflowing the upper limit of its
>>>> type (INT_MAX or SIZE_MAX), I'd probably precompute the last index
>>>> I want to look at and use that to control the loop. But I see no
>>>> point in doing so just to avoid letting i exceed the upper bound
>>>> of the array, when the code explicitly avoids accessing anything
>>>> outside the array bounds.
>>>
>>> No, I was talking about if the loop was for initialization. In such a
>>> case, I'd initialize every element (advancing by 1), but if something
>>> special was needed for every tenth, I could determine that with your 'if'.
>>
>> Who says the loop is for initialization? It might not even be writing
>> to the array.
>
> I did. "The loop body for this could very well initialize ten elements
> at a time, but I'd probably advance 'i' by 1 instead of by 10, and use
> the '% 10' operator in the loop's body, if need be." (If you'll forgive
> the 10 with "operator") I'm saying "if, then", not making a claim about
> the loop's purpose.
My point is that you were making an unwarranted assumption. The example
illustrates iterating over every 10 (or N) elements of an array.
>>> For just _working_ with an already-initialized array, I'd probably use
>>> precisely this loop (though I might possibly be more inclined to put it
>>> in a function and pass the count). BUT, as was snipped, if the count of
>>> 'arr' is 4, then the programmer _really_ ought to have their head
>>> checked. 
>>
>> Absurd.
>>
>> A function that, for whatever reason, needs to access every 10th
>> (or every Nth) element, can perfectly sensibly work on an array of
>> any size. If the array has 10 or fewer elements, the function just
>> accesses element 0. I see no reason to treat that as a special case.
>
> If you are talking about:
>
> void some_func1(void) {
> int arr[4];
> int i;
>
> for (i = 0; i < sizeof arr / sizeof arr[0]; i += 10)
> /* Anything */
> }
>
> then I'd certainly say it's silly! We can see right away that the
> advancement of 'i' is silly! Consider what kind of code review you'd
> offer someone about this.
It's common to use "magic numbers" (4 and 10 in this case) for code
snippets meant to illustrate a point. In real life, clearly the
value would be either a named constant or a value computed at run
time (in the latter case allocating the array is more complicated).
Is that the trivial point you've been trying to make all this time?
Ok, suppose we replace 4 by ARRAY_SIZE, a macro whose value
is determined by some build-time configuration process. Maybe
ARRAY_SIZE happens to be 4.
Or suppose it's a constant value 95, which will result in the final
value of i being 100, past the end of the array.
> If you are talking about:
>
> void some_func1(int * arr, size_t n) {
> int i;
>
> for (i = 0; i < n; i += 10)
> /* Anything */
> }
>
> then I'd certainly say it's not silly at all!
What I *thought* you were objecting to is having i end up with a
value past the last valid index of arr, to the point where you'd
want a compiler to warn you about it. Both code snippets quoted
above have the same non-problem.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"