Click on image to see enlargment

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

            bug445.cpp

1    #include <stdio.h>
2    int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
3    int sum( int a[3][3] )
4        {
5        int i=0, j=0, k=0;
6        for( i = 0; i < 3; i++ )
7            {
8            for( i = 0; i < 3; i++ )
9                {
10               k += a[i][j];
11               }
12           }
13       return k;
14       }
15   int main()
16       {
17       printf( "The triangular sum is %d\n", sum(a) );
18       return 0;
19       }

Jason thought that the 9th triangular number was 45 so he knew that the result printed (12) was wrong. Can you find the problem?


bug445.cpp lint Output

--- Module:   bug445.cpp (C++)
    _
    {
bug445.cpp(4) : Warning 578: Declaration of symbol 'a' hides symbol 'a' (line 2)
              _
    for( i = 0; i < 3; i++ )
bug445.cpp(6) : Info 838: Previously assigned value to variable 'i' has not been used
                               _
        for( i = 0; i < 3; i++ )
bug445.cpp(8) : Warning 445: Reuse of for loop variable 'i' at 'line 6' could cause chaos
    _
    return k;
bug445.cpp(13) : Info 850: for loop index variable 'i' whose type category is
    'integral' is modified in body of the for loop that began at 'line 6'
    _
    }
bug445.cpp(14) : Info 818: Pointer parameter 'a' (line 3) could be declared as
    pointing to const

Reference Manual Explanation


445    reuse of for loop variable 'Symbol' at 'Location' could cause chaos 
       -- A for loop nested within another for loop employed the same loop
       variable.  For example:

                 for( i = 0; i < 100; i++ )
                     {
                     ...
                     for( i = 0; i < n; i++ ) { ... }
                     }


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


Previous Bug - Bug #1717 - September 2010

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 2010, Gimpel Software, All rights reserved.