Author: Dieter Buerssner
Date: 14:01:07 11/04/02
Go up one level in this thread
On November 04, 2002 at 12:41:52, Daniel Clausen wrote:
>What is a "typical" level 4 warning in M$VC? I remove all warnings, even gccs
>warning "last line not terminated with CR". :)
I find even warning level 3 too annoying.
Look at the following very carefully written code:
/* Call with 2 < range <= MY_RAND_MAX+1, returns 0 <= r < range. */
unsigned long rand_rangei(unsigned long range)
{
unsigned long rmax, r, d;
/* find the largest number rmax <= MY_RAND_MAX, for which
(rmax+1) % range == 0.
All returns from rand() > rmax will be skipped, to guarantee
equal probability for all return values. */
d = (MY_RAND_MAX+1U-range) / range + 1;
rmax = d * range - 1; /* -1 to avoid "overflow to zero" */
do
r = PRNG();
while (r > rmax);
return r/d;
}
If MY_RAND_MAX happens to be ULONG_MAX, MY_RAND_MAX+1 will be 0 (this is
guaranteed by the C-Standard). The code still works - actually it is written
with this case in mind. Slightly different formulations can easily be non
portable, or produce wrong result for some values of MY_RAND_MAX and/or range.
But MSVC with warning level 3 will take the fun, and complain about the
overflow.
Warning level 4 will warn about macros:
#define FOO() \
do {statement1; statment2;} while (0)
because of the "while(0)" but not about the inferior:
#define FOO() \
{statement1; statment2;}
The differnce can be seen, when something starts with
if (some_condition)
FOO();
else
{ ... }
The first macro will do the intended thing, and the second one is a synthax
error. I think, more subtle cases are possible.
Regards,
Dieter
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.