C
Charlie Gordon
Richard said:Charlie Gordon said:Richard said:Malcolm McLean wrote:
Which in fact is almost every integer.
Maybe this is true in the sort of programming you do, but there are
many
programs where integers are needed for arithmetic far more than
indexing.
I'm pretty sure that's a perception rather than a reality. Let's say
you are storing a list of amounts of money as integers. You write a
routine to take the average. When asked "what does this routine do"
you will answer "it adds up an amount of money, divides by the count,
and reports it."
Actually that's not what it does.
int average(int *money, size_t N)
{
size_t i;
int total = 0;
for(i=0;i<N;i++)
total += money;
return total/N;
}
Whilst there are two operations that operate on int (total =0, total
+=), there are four which operate on i, and one which operates on
No there are not. it depends what the compiler does. You could as easily
make it 1 one off for initilisation and one for the loop and check
int i=N;
while(i--)
total += money;
total and N. Whether you count C instructions or machine operations,
what the function is mainly doing is calculating a list of indices.
Its not mainly doing that at all. It is mainly accessing and adding up
numbers.
However the programmer's perception will be that he is mainly working
with amounts of money, because that is what is important to his
human-centric way of looking at the routine.
You confuse me by trying to think too much into it.
This function adds a set integers together and then returns the average.
That's what it attempts to do, but the calculation is quite likely to
cause
integer overflow if the array and/or values are large enough, causing
Its not quite likely to do anything of the sort if it is operating in
the limits the designer set - otherwise we would use different types.
undefined behaviour. Otherwise, the result is the average of the values
of
the array, rounded towards zero. If the size N passed is zero, undefined
behaviour is invoked.
I'm not sure I understand why you are writing this. This applies to any
and all code posted here and a code review isn't the issue here.
Anytime someone posts a line like
x+=y;
One could make this comment "that addition might provoke undefined
behaviour if the values are too large".
It is a matter of common sense! Summing an array of unknown size is likely
to cause overflow: the programmer may well have been oblivious to this fact.
the array may contain large numbers, this way of computing the average
requires as a pre-condition that the sum be within bounds of an int type.
This condition is non obvious and should at least be stated in a comment.
Regarding the division by zero, it is a classic bug is this kind of
function. A simple test prevents undefined (and quite likely catastrophic)
behaviour for the case N == 0. Calling this function with N == 0 should be
allowed: N is the number of values to average, not necessarily the size of
the array.
I'm giving a code review for any code posted on c.l.c: I provide advice that
others find useful and informative. I try to use common sense and help
posters avoid classic mistakes. Sometimes it involves deterring them from
using certain problematic functions from the standard C library (strtok,
strncpy...), sometimes I try to help people write more readably, or use less
error prone constructs and algorithms. I think my criticism is on average
more constructive than yours.