Click on image to see enlargment

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

            bug850.cpp

1    #include <stdio.h>
2
3    const int N = 10;
4    int a[N];
5
6    int main()
7        {
8        int n = 0;
9        int i;
10       // prepare an array of ascending
11       // integers:  1, 2, 3, ... , N
12       for( i = 0; i < N; i++ )
13           a[i] = ++n;
14       // so we can compute the Nth Triangular
15       // number: 1 + 2 + 3 + ... + N
16       int sum = 0;
17       for( i = 0; i < N; i++ )
18           sum += a[i++];
19       /* for N == 10 this should be 55 */
20       printf( "%d\n", sum );
21       }

Baltazar, a C++ student, wrote this small program to produce the Nth triangular number. To his dismay he kept getting an unexpected answer. What's wrong?


bug850.cpp lint Output


--- Module:   bug850.cpp (C++)
    _
    printf( "%d\n", sum );
bug850.cpp(20) : Info 850: for loop index variable 'i' whose type category is
    'integral' is modified in body of the for loop that began at 'line 17'

Reference Manual Explanation


850    for loop index variable 'Symbol' whose type category is 'String'
       is modified in body of the for loop that began at 'String'  -- Note:
       This message is delivered after the for loop has been completed.

       A for loop with an identifiable loop index variable was programmed
       in such a way that the loop body also modifies the index variable.
       For example:

                 for( i = 0; i < 100; i++ )
                     {
                     a[i++] = 0;
                     }

       In general it is better to restrict modifications to for loop index
       variables to the for clause if at all possible.  If this is not possible,
       you can prefix the for loop with an appropriate lint comment such as:

                 /*lint -e{850} i is modified in the body of the for loop */

       The message is parameterized with a type category which is one of:

           integral     some form of integer
           float        some form of floating point number
           string       some form of char * including wide char
           pointer      some form of pointer other than string
           enumeration  an enumeration of some kind
           unclassified none of the above

       This will allow you to be more selective in the delivery of messages because
       you may suppress or enable messages according to these classifications.
       For example:

               -e850
               +estring(850,integral)
               +estring(850,float)

       will enable Info 850 for integrals or for floats but not for other forms
       of loop variables.


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


Previous Bug - Bug #693 - May 2009

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