Click on image to see enlargment

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

            bug864.cpp

1    #include <stdio.h>
2
3    int bump( int &x )
4        { return ++x; }
5    int twice( const int &y )
6        { return 2*y; }
7
8    int main()
9        {
10       int z = 0;
11       int k = bump(z) + twice(z);
12       printf( "%d\n", k );
13       return 0;
14       }

A programmer expected that summing the result of incrementing variable z and doubling its value would be 3. That is the value obtained with one compiler but not the value from another. What's going on?


bug864.cpp lint Output

--- Module:   bug864.cpp (C++)
                              _
    int k = bump(z) + twice(z);
bug864.cpp(11) : Info 864: Expression involving variable 'z' possibly depends
    on order of evaluation

Reference Manual Explanation


864    Expression involving variable 'Symbol' possibly depends on order
       of evaluation  -- The variable cited in the message is either
       passed to a reference that is not a const reference or its
       address is passed to a pointer that is not a pointer to const.
       Hence the variable is potentially modified by the function.  If
       the same variable is used elsewhere in the same expression, then
       the result may depend on the order of evaluation of the expression.
       For example:

                 int g( int );
                 int h( int & );
                 int f( int k )
                     {
                     return g(k) + h(k);   // Info 864
                     }

       Here the compiler is free to evaluate the call to g() first with
       the original value of k and then call h() where k gets modified.
       Alternatively, it can, with equal validity, call h() first in
       which case the value passed to g() would be the new value.

       The object being modified could be the implicit argument (the
       this argument) to a member function call. For example:

                 void f( int, int );
                 class X { public: int bump(); int k; };
                 ...
                 X x;
                 f( x.bump(), x.bump() );  // Info 864

       Here the message states that the expression involving object x
       possibly depends on the order of evaluation.  x is an implicit
       argument (by reference) to the bump() member function.  If the
       member function bump() were declared const then the message would
       not have been emitted.

       (See also 11.1 Order of Evaluation in the manual and Warning 564).


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


Previous Bug - Bug #850 - June 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.