Leigh Johnston said:
On 14/03/2011 19:30, Alf P. Steinbach /Usenet wrote:
* MikeP, on 14.03.2011 18:49:
MikeP wrote:
If you investigate the tcmalloc code (by Google), you will find the
following warning:
// NOTE: unsigned types are DANGEROUS in loops and other arithmetical
// places. Use the signed types unless your variable represents a bit
// pattern (eg a hash value) or you really need the extra bit. Do NOT
// use 'unsigned' to express "this value should always be positive";
// use assertions for this.
Is it just their idiom? What's the problem with using unsigned ints
in
loops (it seems natural to do so)? Are C++ unsigned ints "broken"
somehow?
Well that's a relief. Now that I've read the thread posts and the
links
to other threads, I see I have not been doing anything wrong by
preferring unsigned where it seems natural to do so. The biggie points
(for me) are:
1. C++ conversion rules are at the core of the problem.
Yes, right.
Another core issue is monkey-see-monkey-do habits & ideas, that is,
habits & ideas that are not founded in real understanding but are just
mindlessly adopted from some other context where they *are* meaningful.
I.e., that what's meaningful in e.g. Pascal (guaranteed range checking,
advantage) isn't meaningful in C++ (guaranteed lack of range checking,
disadvantage of adopting Pascal habits & ideas).
2.Mixing signed/unsigned inadvertently is not that big of a problem
given
that most compilers emit a warning upon such.
This is, however, wrong.
Particularly so since the Google coder's note speaks of "arithmetical
places".
Compilers generally warn about comparisions between signed and
unsigned,
but can't warn you of equally-or-more dangerous arithmetic constructs.
3. Using signed where unsigned is a better fit, loses semantic value.
Yes, right.
And it's even worse: using signed where unsigned is a better fit
(namely
for bit-level operations), can easily yield Undefined Behavor.
And vice versa, using unsigned where signed is a better fit (namely for
numerical stuff) loses semantic value and can far too easily introduce
bugs.
Bullshit. The concept of a value which is never negative is not
orthogonal to "numerical stuff"; unsigned integral types are not just
for bitwise operations: std::size_t is not normally used in bitwise
operations but it is nevertheless an unsigned integral type.
It's not bullshit, it's a valid opinion that many people agree with.
Your concept of a value which is never negative is not necessarily the
same as everyone elses, for example you think an array can never be
negative but it can be.
double* arr = new double[3];
++arr;
arr is the name/identifier that is used to access the array object.
It's perfectly acceptable to offset the pointer to access the array with
non-zero based indexing. Many algorithms and mathematical functions
benefit from this type of functionality.
You attempt to say the C++ standards disallows this is an attempt to
twist the wording to support your own argument.
If you cannot understand the benefits of non-based array indexing, then
I suggest you are a complete idiot because I'm pretty sure you
unreasonably trying to twist things to suit your argument. And if you
really do think an array cannot be non-zero based then you are a
complete idiot.