goodTweetieBird
2009-08-05 14:45:43 UTC
Our code 'guru' says the that in the snippet below the use of n in the
second loop is unsafe but could not say exactly why, something to do
with the value of n depending on how the loop is exited. Maybe it is
compiler dependent but I ran test cases and could not produce
unexpected behavior. Your opinions would be appreciated.
Thanks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int i, n;
// Cycle thru arbitration array until an empty slot is found or the
end is reached.
for (n = 0; n < ARB_ARRAY_SIZE; n++)
{
if (arbVal[n].ipAddress == 0)
{
break;
}
arbVal[n].clockQuality = calculateClockQualityMux(arbVal
[n].fpgaStatus);
}
// A modified 'selection sort' routine.
// Does not run if n == 0;
for(i = 0; i < n; i++)
{
...
}
second loop is unsafe but could not say exactly why, something to do
with the value of n depending on how the loop is exited. Maybe it is
compiler dependent but I ran test cases and could not produce
unexpected behavior. Your opinions would be appreciated.
Thanks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int i, n;
// Cycle thru arbitration array until an empty slot is found or the
end is reached.
for (n = 0; n < ARB_ARRAY_SIZE; n++)
{
if (arbVal[n].ipAddress == 0)
{
break;
}
arbVal[n].clockQuality = calculateClockQualityMux(arbVal
[n].fpgaStatus);
}
// A modified 'selection sort' routine.
// Does not run if n == 0;
for(i = 0; i < n; i++)
{
...
}