delphi - Should I use unsigned integers for counting members? -
Should I use unsigned integer for my counting class members?
Answer
For example, eclipse a class
TList & lt; T & gt; = Class Private FQ: Cardinal; Public Property Count: Read Cardinal FCount; End; It makes sense, is not it? The number of items stored in the list can not be negative, so why not use an unsigned integer type for this? I think it is generally a good theory to always use at least the usual (the most special type of the most special type) type.
Now, walking on a list looks like this:
I: = 0 for the list. Calculation - 1 double list (list [i]); When the number of items stored in the list is zero, then it tries to evaluate the compiler
list.count - 1< / Pre>which is a good integer overflow (underflow is accurate). Combined with the fact that debugger does not show the proper location where the exception occurred, it was very difficult for me.
I should say that if you have overflow check closed, then it can also be difficult to track errors as you will often use memory which is not for you - and it results from The result is in the behavior.
Now I am using plain interses for all my counting members to avoid situations like this.
If this is utter nonsense, please let me know :)
(I used to track an integer overflow in my code in just one hour, so I decided to share it Most people will definitely know here, but maybe I can save some time.)
No, certainly Not from Delphi idioms are here to use integer Do not fight with language You will not have more elements in the list in 32-bit surroundings, except that you try to create a bitmap.
We are clear: Every programmer who is using your code will hate you to use the cardinal instead of an integer.
Comments
Post a Comment