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

bug1547.cpp

1    class Object { public: /* ... */ virtual double Evaluate(); };
2    class AnimalObject : public Object { int animation; };
3
4    double sum( Object ob[], int n )
5        {
6        double r = 0;
7        for( int i = 0; i < n; i++ )
8            r += ob[i].Evaluate();
9        return r;
10       }
11
12   AnimalObject aoa[1000];           /* animal object array */
13   double aoa_s = sum( aoa, 1000 );  /* the sum of its members */

This is a section of a larger program which is giving mysterious access violations. Can you spot the difficulty?

bug1547.cpp lint Output

--- Module:   bug1547.cpp
                       _
double aoa_s = sum( aoa, 1000 );  /* the sum of its members */
bug1547.cpp  13  Warning 1547: Assignment of array to pointer
    to base class (arg. no. 1)

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).  [23, Item 3].

       We do not warn about the assignment to q because it appears that
       the programmer realizes the situation and wishes to confine q to
       the base object of the zeroeth element of a only.  As a further
       precaution against inappropriate array access, out of bounds
       warnings are issued for subsequent references to p[1] and q[1].

Previous Bug - Bug #671 - December 1997
Next Bug - Bug #661 - February 1998

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright © 2003, Gimpel Software