Click on image to see enlargment

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

            bug1570.cpp

1    #include <stdio.h>
2    int pumpkins[10];
3    struct Arrayer
4        {
5        int &n;
6        int cnt;
7        Arrayer(int k, int c) : n(k), cnt(c) {}
8        void triangulate()
9            {
10           for( int i = 0; i < cnt; i++ )
11               *(&n+i) = i;
12           }
13       };
14   int main()
15       {
16       Arrayer a(pumpkins[0], 10);
17       a.triangulate();
18       for( int i = 0; i < 10; i++ )
19           printf( "%d ", pumpkins[i] );
20       printf( "\n" );
21       return 0;
22       }

Rudy the ref-maven is writing a class to centralize array manipulations but is distressed as his triangulate function is seriously misbehaving. Aside from aesthetics, what is Rudy's problem? Hint: it can be resolved by the addition of one character


bug1570.cpp lint Output

--- Module:   bug1570.cpp (C++)
          _
    int &n;
bug1570.cpp(5) : Info 1725: class member 'Arrayer::n' is a reference 
                               _
    Arrayer(int k, int c) : n(k), cnt(c) {}
bug1570.cpp(7) : Warning 1570: Initializing a reference class member with an
    auto variable 'k' 
    _
    };
bug1570.cpp(13) : Info 1712: default constructor not defined for class 'Arrayer'

Reference Manual Explanation


1570   Initializing a reference class member with an auto variable 'Symbol'  -- In
       a constructor initializer, a reference class member is being initialized to
       bind to an auto variable.  Consider:

             class X { int &n; X(int k) :n(k) {} };

       In this example member n is being bound to variable k which, although a parameter,
       is nonetheless placed into auto storage. But the lifetime of k is only the duration
       of the call to the constructor, whereas the lifetime of n is the lifetime of the
       class object constructed.




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


Previous Bug - Bug #1733 - September 2011

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