Re: What *is* a CtrlID?
- From: "Giovanni Dicanio" <giovanniDOTdicanio@xxxxxxxxxxxxxxxxx>
- Date: Tue, 20 Jan 2009 10:15:26 +0100
"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
.
- Follow-Ups:
- Re: What *is* a CtrlID?
- From: Joseph M . Newcomer
- Re: What *is* a CtrlID?
- From: Giovanni Dicanio
- Re: What *is* a CtrlID?
- References:
- What *is* a CtrlID?
- From: .rhavin grobert
- Re: What *is* a CtrlID?
- From: Joseph M . Newcomer
- What *is* a CtrlID?
- Prev by Date: Re: Crash using C++/CLI to let MFC use .Net
- Next by Date: Re: What *is* a CtrlID?
- Previous by thread: Re: What *is* a CtrlID?
- Next by thread: Re: What *is* a CtrlID?
- Index(es):
Relevant Pages
|