Click on image to see enlargment

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

            bug1567.mm

@header barroom.h
1      #include <stdlib.h>
2      #include <math.h>
3      typedef double Dbl;
4      extern Dbl volume;
 
@module volume.cpp
1      #include "barroom.h"
2      Dbl get_volume()
3        {
4        char *numeral = getenv("VOLUME");
5        if( numeral ) return atof(numeral);
6        return 0;
7        }
8      Dbl volume = get_volume();
 
@module price.cpp
1      #include "barroom.h"
2      Dbl get_price( Dbl cost )
3        { return cost * (2 + pow(.9,volume) ); }
4      Dbl price_drink = get_price( 1.25 );

Bartender Barry is preparing for a big New Year's Eve bash by computerizing his bar room. This is a small multi-module segment of a much larger program which uses an environment variable to assist in initialization. But he's getting inconsistent results. Can you find his mistake?


bug1567.mm lint Output

--- Module: volume.cpp (C++)
     1    #include "barroom.h"
Including file barroom.h (hdr)
         1    #include <stdlib.h>
         2    #include <math.h>
         3    typedef double Dbl;
         4    extern Dbl volume;
Resuming file volume.cpp
     2    Dbl get_volume()
     3      {
     4      char *numeral = getenv("VOLUME");
     5      if( numeral ) return atof(numeral);
     6      return 0;
     7      }
     8    Dbl volume = get_volume();

--- Module: price.cpp (C++)
     1    #include "barroom.h"
Including file barroom.h (hdr)
         1    #include <stdlib.h>
         2    #include <math.h>
         3    typedef double Dbl;
         4    extern Dbl volume;
Resuming file price.cpp
     2    Dbl get_price( Dbl cost )
     3      { return cost * (2 + pow(.9,volume) ); } 
     4    Dbl price_drink = get_price( 1.25 );
Warning 1567:  Initialization of variable 'price_drink' (line 4, file price.cpp)
               is indeterminate as it uses variable 'volume' through calls: 'get_price()'

Reference Manual Explanation


1567   Initialization of variable 'Symbol' (Location) is indeterminate
       as it uses variable 'Symbol' through calls: 'String'  -- A
       variable was dynamically initialized using an expression that
       contained a call to a function and that function referenced a
       variable that was also dynamically initialized and was in some
       other module.  For example:

                 a.cpp:                  b.cpp:

                 int g(void);            int f(void);
                 int y = g();            int x = f();
                 int f() { return y; }

       The initialization of both x and y are dynamic.  Although the
       order of dynamic initialization within a module is pre-ordained
       the order in which modules are initialized is not.  Therefore it
       is perfectly possible for b.cpp to be initialized before a.cpp.
       Thus when the call is made upon function f() to initialize x,
       variable y may not yet have been initialized.


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


Previous Bug - Bug #1563 - December 2010

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.