Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug573.cpp

1    #include <stdio.h>
2
3    const int march[31] = {
4        8, 5, 7, 2, -4, -14,  -7, -4, -2,  0,
5        0, 2, 5, 7,  2,  -4, -14, -7, -4, -2,
6        1, 7, 2, 2, -2,  -3,  -4,  6, -4,  3, 9 };
7
8    int main()
9        {
10       unsigned i, count = 31;
11       int sum = 0;
12
13       for( i = 0; i < count; i++ )
14           {
15           sum += march[ i ];
16           }
17       printf( "The average low temperature in March was"
18               " %d degrees\n", sum / count );
19       return 0;
20       }

Temperature readings from Point Barrow, Alaska, give an unusually high value for the average temperature in March. Try as they might they could not attribute this to global warming. Could the bits and bytes be frozen? What's going on?


bug573.cpp lint Output

--- Module:   bug573.cpp (C++)
                                         _
            " %d degrees\n", sum / count );
bug573.cpp(18) : Warning 573: Signed-unsigned mix with divide
bug573.cpp(18) : Info 737: Loss of sign in promotion from int to unsigned int

Reference Manual Explanation


573    Signed-unsigned mix with divide  -- one of the operands to / or %
       was signed and the other unsigned; moreover the signed quantity
       could be negative.  For example:

             u / n

       where u is unsigned and n is signed will elicit this message
       whereas:

             u / 4

       will not, even though 4 is nominally an int.  It is not a good
       idea to mix unsigned quantities with signed quantities in any
       case (a 737 will also be issued) but, with division, a negative
       value can create havoc. For example, the innocent looking:

             n = n / u

       will, if n is -2 and u is 2, not assign -1 to n but will assign
       some very large value.

       To resolve this problem, either cast the integer to unsigned if
       you know it can never be less than zero or cast the unsigned to
       an integer if you know it can never exceed the maximum integer.


If you have comments or questions about this bug, please post them to our   Discussion Forum


Previous Bug - Bug #1506 - February 2007

[ new ] Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2007, Gimpel Software, All rights reserved.