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

            bug1732.cpp

1     #include <iostream.h>
2
3     struct WhiteHouse
4         {
5         int *p;
6         WhiteHouse(int n) { p = new int; *p = n; }
7         ~WhiteHouse() { delete p; }
8         };
9     WhiteHouse ww(1912);
10    void f() { WhiteHouse fdr(1932); fdr = ww; }
11    int main()
12        {
13        f();
14        WhiteHouse gwb(2000);
15        cout << *ww.p;
16        return 0;
17        }

This program has a serious flaw. With one compiler it printed "2000"; with another it produced an 'illegal operation'. Can you spot the bug?


bug1732.cpp lint Output

--- Module:   bug1732.cpp
                            _
    WhiteHouse(int n) { p = new int; *p = n; }
bug1732.cpp(6) : Info 1732: new in constructor for class 'WhiteHouse' which has
    no assignment operator
bug1732.cpp(6) : Info 1733: new in constructor for class 'WhiteHouse' which has
    no copy constructor
    _
    };
bug1732.cpp(8) : Info 1712: default constructor not defined for class
    'WhiteHouse'

Reference Manual Explanation

 
1732  new in constructor for class 'Name' which has no
      assignment operator  -- Within a constructor for the cited
      class, there appeared a new.  However, no assignment
      operator was declared for this class.  Presumably some
      class member (or members) points to dynamically allocated
      memory.  Such memory is not treated properly by the
      default assignment operator.  Normally a custom assignment
      operator would be needed.  Thus, if x and y are both of
      type Name

                x = y;

      will result in pointer duplication.  A later delete would
      create chaos.   [12, Item 11]

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

Previous Bug - Bug #1544 - December 2002

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

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