In <3ef99341$0$8262$> "Simon Biber" <> writes:
>"Dan Pop" <> wrote:
>> "Simon Biber" <> writes:
>> > There is very good reason for the warning here as it
>> > has uncovered a potential bug in your code. If the
>> > value returned by strlen is greater than INT_MAX,
>> > your program has undefined behaviour due to overflow.
>>
>> Since when is allowed INT_MAX to be less than 5? I may
>> have more information than the compiler, therefore the
>> compiler must trust me.
>
>I said "potential bug". There is no bug in your code,
>but in most cases when looping through a string there
>is the possibility of it being of any length, and it
>is quite possible it could exceed INT_MAX.
But the point is that I have additional information that rules out this
*theoretical* possibility. Therefore, there is no good reason for using
size_t.
Apart from that, when was the last time you used a string whose size
exceeded INT_MAX, so that the "quite possible" in your assertion is
justified?
>You clearly like to write code that gets the job done but
>leaves no room for expansion or re-use. I prefer to code
>in the most robust way available.
Bullshit. I've never had any problem reusing or expanding my code.
If I decide, at design time, that the right type for a variable is int,
then I know what I'm doing. The compiler has no business to question my
decision.
>> >You should always use size_t for loop iterators whose
>> >bound is expressed in size_t. This includes arrays
>> >when you use
>> > sizeof foo / sizeof *foo
>> >as the bound.
>>
>> Sheer bullshit! If I *know* that int is enough, there's
>> NO reason for using size_t.
>
>Again, you know that int is enough now, but who knows what
>changes you or other people may wish to make to your code
>in the future? It is better to do things properly now so
>that future expansion will be painless.
You're missing the point! Unsigned variables have plenty of pitfalls
and one of them could affect the future changes (especially if they are
done by someone with less experience). By choosing the type int, where
it is the *right* type, I'm ensuring that even a non-experienced
programmer can maintain the code. If you still can't see my point,
replace strlen(something) by sizeof(double) in my original example.
In theory, sizeof(double) could exceed 32767, yet I'm perfectly willing
to ignore this possibility.
>> >In my experience externally imposed interfaces are uncommon,
>>
>> Have a look at the specification of signal() sometime.
>
>Name some common, portable, standard-conforming uses of signal().
#include <signal.h>
volatile sig_atomic_t interrupt;
void handler(int signo)
{
interrupt = 1;
}
....
signal(SIGINT, handler);
signal(SIGTERM, handler);
>> >and I see the odd cast to void as useful self-documentation.
>> >It lets the reader of the code immediately see that you
>> >intentially do not use the value of the argument.
>>
>> Or he might start wondering why I am doing such a silly thing
>> as casting an otherwise unused argument to void.
>
>The cast to void is a fairly well-known signal for "I don't
>care what value this expression has".
Chapter and verse, please. Or other source for this widespread knowledge.
>> If I don't use the value of the argument (as is usually the
>> case with my signal handlers), what is the obvious conclusion?
>
>If the function is long and/or complicated it may not be obvious
>at a glance whether or not each argument is actually used.
Why should anyone care?
>Furthermore the non-use of an argument is so often a mistake that
^^^^^^^^
>a reader is forced to reevaluate whether or not you really did
>mean to not use it the argument.
Care to produce some supporting data for "so often"? I've never ignored
an argument by mistake, each and every time it was a deliberate decision.
>> > But there is a very good reason to use a size_t
>> > variable here!
>>
>> Nope! Single letter variables are often reused as loop
>> counters and temporary variables. More often than not,
>> the type int is more appropriate than an unsigned type
>> and the decision should be mine, not compiler's, anyway.
>
>Variables should be reduced to the minimum scope necessary.
That would mean opening plenty of blocks for no good reason. IIRC, it
was E.R. Tisdale advocating this approach.
>Single letter variables should not be reused, this creates
>confusion over whether their value is significant between
>uses.
No such confusion is possible, when the reuse starts be assigning a new
value. It would be downright idiotic to use a different loop control
variable for each independent loop in a function.
>I like the for(type i=0; i<n; i++) form from C99, where the
>variable goes out of scope immediately on exit from the loop.
It's non-portable. Some people do care about portability, even if you
don't.
>> >So, I would write:
>> > #include <string.h>
>> >
>> > void foo(int arg)
>> > {
>> > (void)arg;
>> > for (size_t i = 0, n = strlen("abcde"); i < n; i++)
>> > {
>> > /* do something */
>> > }
>> > }
>> >for which `gcc -c -std=c99 -pedantic -Wall -W -O2 warn.c`
>> >gives no warnings.
>>
>> That's precisely my point: why bother with options that
>> *force* you to code around them?
>
>I don't feel forced to code around it! I believe it is good
>style to code in such a way that the warnings don't come up.
I don't. While the unused argument issue is harmless, the gratuitous
usage of unsigned variables is NOT.
>> What guarantee do you have that *another* compiler
>> won't complain about the useless statement (void)arg; ?
>
>None; a compiler is allowed to complain about anything.
>However, in my experience compilers universally take a
>cast as a sign to shut up.
Is it "your experience" or "universally"?
>> Apart from that, your code is not portable to *most* C
>> compilers, for no *good* reason at all.
>
>Bogus. My code is portable to *all* C compilers.
By a bogus definition of C. Your code is non-portable to most of
the real world C compilers, period.
>Most of the compilers that used to be C compilers,
>are now obsolete. C99 replaced C89. C89 is not C.
You're severely confused. One ISO standard replaced another. Up to now,
this had no significant impact either on the industry or on the academia.
The C programming community at large is ignoring the C99 specification.
Furthermore, as attested by comp.std.c, copies of the ISO C90 standard
are still in constant demand (and at least one national standardisation
organisation, BSI, is currently selling them).
The value of an ISO standard is determined by its impact on the industry
that's supposed to use it. Until now, C99 had practically none.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: