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

            bug1747.cpp

1     class Complex
2         {
3         public:
4         double real, imag;
5         Complex();
6         };
7
8     Complex& operator+( Complex& a, Complex& b)
9         {
10        Complex *p = new Complex;
11        p->real = a.real + b.real;
12        p->imag = a.imag + b.imag;
13        return *p;
14        }

Shown above is just a portion of a larger program. The programmer's lament is that he keeps running out of storage when summing up large arrays of complex numbers. What's his problem?


bug1747.cpp lint Output

Module:   bug1747.cpp
    _
    {
bug1747.cpp  9  Info 1747: binary operator 'operator+(Complex &, Complex &)'
    returning a reference
             _
    return *p;
bug1747.cpp  13  Warning 429: Custodial pointer 'p' (line 10) has not been
    freed or returned

--- Global Wrap-up

Info 1754: Expected symbol 'operator+=' to be declared for class 'Complex'


Reference Manual Explanation

 
1747   binary operator 'Symbol' returning a reference  -- An
       operator-like function was found to be returning a reference.
       For example:

                 X &operator+ ( X &, X & );

       This is almost always a bad idea.  [12, Item 23].  You normally
       can't return a reference unless you allocate the object, but then
       who is going to delete it.  The usual way this is declared is:

                 X operator+ ( X &, X & );


Previous Bug - Bug #602 - February 2001

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

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