On 03/10/2013 07:00 AM, Henri Verbeet wrote:
On 10 March 2013 08:20, larmbr zhan <nasa4...@gmail.com> wrote:

    But It behaves different when it is signed or not.  According to
    C Standard,
>>
    -  For the signed case, once it overflows, resulting in
       representing a  negative value .

Actually, signed overflow behavior is undefined according to the
standard.

Specifically, some hardware throws an exception on signed arithmetic
overflow and C is specifically designed to be hardware independent, so
it has to be 'undefined behavior'.

       -  For the unsigned case,  once it overflows, resulting in
          representing a value reduced modulo the largest value
          that object could hold.

Nit: modulo the largest value the object can hold _plus one_, but it should be treated as another 'undefined behavior'.

To further complicate the issue, while size_t is always unsigned, size_t can be 16, 18, 32, 36 or 64 bits and still be compliant. So use
'size_t' when you are talking about the size of something, and
'unsigned' for flag sets and counts.


Reply via email to