Click on image to see enlargment

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

            bug1529.cpp

1    #include <stdio.h>
2    #include <string.h>
3
4    struct Card {
5        char *msg;
6        Card() { msg = new char[100]; strcpy( msg, "hello world" ); }
7        Card &operator=( const Card & x )
8            { delete[] msg;  msg = new char[ strlen(x.msg) + 1 ];
9              strcpy( msg, x.msg ); return *this; }
10       ~Card() { delete[] msg; }
11       };
12
13   void process( Card &x1, Card &x2 ) { x1 = x2; }
14
15   int main()
16       {
17       Card b;
18       process( b, b );
19       printf( "%s\n", b.msg );
20       return 0;
21       }

Sam Smily wants to automate his greeting card company but is having difficulty getting started. His program compiles and runs but his message is not getting out. What's going wrong?


bug1529.cpp lint Output

--- Module:   bug1529.cpp (C++)
                   _
    Card() { msg = new char[100]; strcpy( msg, "hello world" ); }
bug1529.cpp(6) : Info 1733: new in constructor for class 'Card' which has no copy constructor 
                                              _
          strcpy( msg, x.msg ); return *this; }
bug1529.cpp(9) : Warning 1529: Symbol 'Card::operator=(const Card &)' not first checking for
    assignment to this 
                                              _
void process( Card &x1, Card &x2 ) { x1 = x2; }
bug1529.cpp(13) : Info 1764: Reference parameter 'x2' (line 13) could be declared const ref

Reference Manual Explanation


1529   Symbol 'Symbol' not first checking for assignment to this  -- The assignment operator does not
       appear to be checking for assignment of the value of a variable to itself (assignment to this).
       Specifically PC-lint/FlexeLint is looking for one of:

                   if( &arg == this )
                   if( &arg != this )
                   if( this == &arg )
                   if( this != &arg )

       as the first statement of the function.

       It is important to check for a self assignment so as to know whether the old value should be subject
       to a delete operation.  This is often overlooked by a class designer since it is counter-intuitive
       to assign to oneself.  But through the magic of aliasing (pointers, references, function arguments)
       it is possible for an unsuspecting programmer to stumble into a disguised self-assignment.
       If you are currently using the following test

                   if( arg == *this)

       we recommend you replace this with the more efficient:

                   if( &arg == this || arg == *this)


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


Previous Bug - Bug #602 - August 2012

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 2012, Gimpel Software, All rights reserved.