Why are unsigned integers error prone?

后端 未结 8 1100
眼角桃花
眼角桃花 2020-12-13 08:28

I was looking at this video. Bjarne Stroustrup says that unsigned ints are error prone and lead to bugs. So, you should only use them when you really need t

8条回答
  •  感动是毒
    2020-12-13 08:45

    Numeric conversion rules in C and C++ are a byzantine mess. Using unsigned types exposes yourself to that mess to a much greater extent than using purely signed types.

    Take for example the simple case of a comparison between two variables, one signed and the other unsigned.

    • If both operands are smaller than int then they will both be converted to int and the comparison will give numerically correct results.
    • If the unsigned operand is smaller than the signed operand then both will be converted to the type of the signed operand and the comparison will give numerically correct results.
    • If the unsigned operand is greater than or equal in size to the signed operand and also greater than or equal in size to int then both will be converted to the type of the unsigned operand. If the value of the signed operand is less than zero this will lead to numerically incorrect results.

    To take another example consider multiplying two unsigned integers of the same size.

    • If the operand size is greater than or equal to the size of int then the multiplication will have defined wraparound semantics.
    • If the operand size is smaller than int but greater than or equal to half the size of int then there is the potential for undefined behaviour.
    • If the operand size is less than half the size of int then the multiplication will produce numerically correct results. Assigning this result back to a variable of the original unsigned type will produce defined wraparound semantics.

提交回复
热议问题