Click on image to see enlargment

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

            bug1416.cpp

1    #include <math.h>
2    #include <stdio.h>
3    double zero = 0, three = 3, four = 4;
4    class Complex
5        {
6        public:
7        double &x, &y;
8        Complex() : x(zero), y(zero) { }
9        Complex( double &z ) : x(y), y(z) { }
10       Complex( double &_x, double &_y ) : x(_x), y(_y) { }
11       };
12   double square( double const &x )
13       { return x * x; }
14   double absValue( Complex &c )
15       { return sqrt( square(c.x)+square(c.y) ); }
16   int main()
17       {
18       Complex c(four);
19       printf( "absolute value is %g\n", absValue( c ) );
20       return 0;
21       }

In an effort to understand references, a student wrote this program that is chock full of references, even class members. But his program is producing inconsistent results with different compilers. Can you spot the problem?


bug1416.cpp lint Output

--- Module:   bug1416.cpp (C++)
             _
    double &x, &y;
bug1416.cpp(7) : Info 1725: class member 'Complex::x' is a reference
bug1416.cpp(7) : Info 1725: class member 'Complex::y' is a reference
                              _
    Complex( double &z ) : x(y), y(z) { }
bug1416.cpp(9) : Warning 1416: An uninitialized reference 'Complex::y' is being
    used to initialize reference 'Complex::x'  
                                              _
    { return sqrt( square(c.x)+square(c.y) ); }
bug1416.cpp(15) : Info 1764: Reference parameter 'c' (line 14) could be
    declared const ref

Reference Manual Explanation


1416   An uninitialized reference 'Symbol' is being used to initialize
       reference 'Symbol'  -- This message is usually issued when a
       reference to a member of a class is used to initialize a
       reference to another member of the same class before the first
       member was initialized.  For example:

                 class C
                     {
                     int &n, &m;
                     C( int &k ) : n(m), m(k) { /* ... */ }
                     }; 
Here m is initialized properly to be identical to k. However, the initialization of n, taking place, as it does, before m is so initialized, is erroneous. It is undefined what location n will reference.


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


Previous Bug - Bug #583 - December 2008

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.