Click on image to see enlargment

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

            bug1547.cpp

1    class Drink { public: /* ... */ virtual bool spiked(); };
2    class Martini : public Drink { double gin, vermouth; };
3    const int all_bad = -1;
4
5    int scan( Drink drinks[], int n )
6        {
7        for( int i = 0; i < n; i++ )
8            if( !drinks[i].spiked() ) return i;
9        return all_bad;
10       }
11
12   Martini drinks[100];
13   int select = scan( drinks, 100 );

The evil Dr. Yes invites one James Bond to a New Year's Eve celebration and places before him an enticing array of Martinis. But Bond's spike detector, an elaboration on the code above, is simply not working as expected. Can you spot the flaw?


bug1547.cpp lint Output

--- Module:   bug1547.cpp (C++)
                         _
int select = scan( drinks, 100 );
bug1547.cpp  14  Warning 1547: Assignment of array to pointer to base class (arg. no. 1)
bug1547.cpp  14  Warning 1552: Converting pointer to array-of-derived to pointer to base

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

Info 754: local structure member 'Martini::gin' (line 3, file bug1547.cpp) not referenced

Reference Manual Explanation


1547   Assignment of array to pointer to base class (Context)  -- An assignment
       from an array of a derived class to a pointer to a base class was detected.
       For example:

                 class B { };
                 class D : public B {};
                 D a[10];
                 B *p = a;      // Warning 1547
                 B *q = &a[0];  // OK

       In this example p is being assigned the address of the first element of an
       array.  This is fraught with danger since access to any element other than
       the zeroeth must be considered an error (we presume that B and D actually
       have or have the potential to have different sizes).


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


Previous Bug - Bug #1402 - December 2007

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.