Click on image to see enlargment

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

            bug1401.cpp

1    #include <stdio.h>
2    class Drink
3        { public:
4        enum Ice { shaken, stirred } ice;
5        float vermouth, gin, scotch;
6        };
7    class Martini : public Drink
8        { public: Martini()
9            { vermouth = .10; gin = .90; }
10       };
11   class BondMartini: public Martini
12       { public:
13         BondMartini() { ice = shaken;
14           gin += vermouth / 2; vermouth /= 2; }
15       };
16   int main()
17       {
18       BondMartini m;
19       printf( "%d %g %g %g\n", m.ice, m.gin,
20                       m.vermouth, m.scotch );
21       return 0;
22       }

For New Year's Eve, James Bond orders his usual martini but has it analyzed first. One of the ingredients is suspiciously off. Which one?


bug1401.cpp lint Output

--- Module:   bug1401.cpp (C++)
                                     _
        { vermouth = .10; gin = .90; }
bug1401.cpp(9) : Warning 1401: member 'Drink::ice' (line 4) not initialized by constructor
                                     _
        { vermouth = .10; gin = .90; }
bug1401.cpp(9) : Warning 1401: member 'Drink::scotch' (line 5) not initialized by constructor

    --- Wrap-up for Module: bug1401.cpp

Info 749: local enumeration constant 'Drink::Ice::stirred' (line 4, file bug1401.cpp) not referenced

Reference Manual Explanation


1401   member 'Symbol' (Location) not initialized by constructor -- The indicated
       member symbol was not initialized by a constructor.  Was this an oversight?

Commentary

We've been asked to comment on the bug of the month, in addition to providing exactly and precisely what our lint product and its accompanying documentation gives you. So here goes:

For one thing, this example demonstrates a shortcoming of this and all other static analyzers. The problem, of course, is that James Bond preferred Vodka Martinis and not Gin Martinis

Aside from this glaring error, we have one stemming from the subtleties and bewildering varieties of initialization in C++. C++ supports copy initialization, direct initialization, dynamic initialization, default initialization, explicit initialization, no initialization, static initialization, value initialization and zero initialization, and it's easy to forget which applies in a given situation. In Mr. Bond's case, "no initialization" would apply to the amount of scotch in the drinks and so this component's value resembles noise.

When we ran the program we got the values:

0 0.95 0.05 5.88923e-039



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


Previous Bug - Bug #774 - December 2006

[ new ] 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 2007, Gimpel Software, All rights reserved.