Re: What *is* a CtrlID?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Note that the code you wrote should result in a compiler warning. It was a bug we knew
about in 1969. That's 40 years ago. You don't test for >= 0 on unsigned numbers.

But that doesn't explain why you can have negative lengths for strings, or copy
operations, etc. This dates back to the days when C only had one type, 'int', which it
used EVERYWHERE. Then someone pointed out that unsigned int was a useful concept, and
they added the unsigned qualifier, but left all the libraries using int, because unsigned
int was really the same as int, so there was no need to change. Then other people who
needed lengths decided that int made sense because, well, everyone else used signed int,
so why not?

Remember: software evolves. We know this because there is little trace of intelligent
design in it.
joe

On Tue, 20 Jan 2009 10:15:26 +0100, "Giovanni Dicanio"
<giovanniDOTdicanio@xxxxxxxxxxxxxxxxx> wrote:


"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> ha scritto nel messaggio
news:16ban49c253ov4c0q0q6f7iqtechtdq64f@xxxxxxxxxx

Microsoft is terribly confused about negative numbers, because they
started using Win16
and never really understood the qualifier 'unsigned'. How is it possible
to have a signed
value for the length of a string or an array? This could only make sense
if you could
have a negative length.

Joe: for the signed/unsigned thing you mentioned here, I must confess that
considering my (much smaller than yours) experience, I came to the
conclusion of prefering the signed integers for loops, etc.

In the past I was hit by some bugs in some code, like this:

<code>

unsigned count;
...

while (--count > 0)
{
// do something
...
}

</code>

This is an infinite loop (and it is subtle to detect, especially if you are
not an experienced programmer, if this is the first time you see that bug,
and if this loop is immersed in other complex "real world" code).
The key in that bug is the 'unsigned' count variable. If you use just the
*signed* 'int', everything is fine.

There are also other cases similar to that shown above.

Basically, I found that just using signed integer ('int') even for data that
is unsigned in his nature (like a length of a string), saves from subtle
bugs.
(At least, if there is some data that is unsigned in nature, I like putting
an ASSERT like ASSERT( someLength > 0 ); but store this variable in a signed
'int'.)

And if there is a need to store values bigger than 2^31-1 = 2,147,483,647
(the maximum positive value a 32-bit int can store), I would use a 64-bit
signed integer.

Of course, I'm open to listen to your experiences and insights about that.

Thanks,
Giovanni

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • [PATCH] Memory management livelock
    ... Here is a patch for MM livelock. ... The original bug report follows after the ... I declared a mutex starvation_barrier in struct ... int nr_pages; ...
    (Linux-Kernel)
  • Re: [Bug #12263] Sata soft reset filling log
    ... The following bug entry is on the current list of known regressions ... int tries = 5; ... * @buf: data buffer ...
    (Linux-Kernel)
  • Re: [PATCH 2/2] tracing/events/lockdep: move tracepoints within recursive protection
    ... RSP ... Either there's a bug in the vbin_printf, or we have some crazy lock->name? ... TP_PROTO(struct lockdep_map *lock, unsigned int subclass, ... The trace points in lockdep are called all the time. ...
    (Linux-Kernel)
  • Re: [PATCH] Avoid buffer overflows in get_user_pages()
    ... In particular, "len" is a signed int, and it is only checked at the ... So, if it is passed in as zero, the loop ... I think that, if get_user_pageshas been asked to grab zero pages, ... Which is a bug, and you want to catch it. ...
    (Linux-Kernel)
  • Re: pointers?
    ... int a; ... Well, not a bug IMO, but yes, that's what I'm talking about. ... Foo is called with delta as parameter, ... compiler "optimizes" b away, therefore the compiler has a bug. ...
    (comp.lang.forth)