Re: What *is* a CtrlID?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Tue, 20 Jan 2009 14:17:11 -0500
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 [MVP]
"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
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- What *is* a CtrlID?
- From: .rhavin grobert
- Re: What *is* a CtrlID?
- From: Joseph M . Newcomer
- Re: What *is* a CtrlID?
- From: Giovanni Dicanio
- What *is* a CtrlID?
- Prev by Date: Re: A (basic) problem with edit control
- Next by Date: Re: Deaf CAsyncSocket on Windows Service.
- Previous by thread: Re: What *is* a CtrlID?
- Next by thread: How make application on programbar flash
- Index(es):
Relevant Pages
|