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

            bug1544.cpp

1     // bug1544.cpp
2
3     #include <iostream.h>
4
5     extern int f(void);
6     extern int b;
7
8     int a = f();
9
10    int main()
11        {
12        cout << b;
13        return 0;
14        } 
 
1     // bug1544b.cpp
2
3     extern int a;
4     int b = a;
5
6     int f()
7         {
8         return 3;
9         } 

You might expect that this two-module program would print "3". Sometimes it does, but it has a problem. Can you spot it?


bug1544.cpp lint Output

--- Module:   bug1544.cpp

--- Module:   bug1544b.cpp
         _
int b = a;
bug1544b.cpp(4) : Warning 1544: Value of variable 'a' (line 3) indeterminate
    (order of initialization)

Reference Manual Explanation

 
1544  value of variable 'Symbol' (Location) indeterminate (order
      of initialization)  -- A variable (identified by Symbol)
      was used in the run-time initialization of a static
      variable.  However this variable itself was initialized at
      run-time.  Since the order of initialization cannot be
      predicted this is the source of possible error.

      Whereas addresses are completely known at initialization
      time values may not be.  Whether the value or merely the
      address of a variable is used in the initialization of a
      second variable is not an easy thing to determine when an
      argument is passed by reference or via pointer.  For example,

                class X
                   {
                   X( const X & );
                   };

                extern X x1;
                X x2 = x1;
                X x1 = x2;

      It is theoretically possible, but unlikely, that the
      constructor X() is interested only in the address of its
      argument and not its current value.  If so, it only means
      you will be getting a spurious report, which you can
      suppress based on variable name.  However, if the const is
      missing when passing a reference parameter (or a pointer
      parameter) then we cannot easily assume that values are
      being used.  In this case no report will be issued.  The
      moral is that if you want to get the checking implied by
      this message you should make your constructor reference
      arguments const.

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

Previous Bug - Bug #557 - November 2002

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2003, Gimpel Software