Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

CWARN.COPY.NOASSIGN

Class defines copy constructor, but no assignment operator

The CWARN.COPY.NOASSIGN checker finds classes that define a copy constructor, but no assignment operator.

Vulnerability and risk

The default copy constructor and assignment operator generated by the C++ compiler perform a bit-wise copy of class data members. If members are overwritten, memory might not be disposed of properly. This situation can lead to leaked memory and unreleased resources.

Mitigation and prevention

To address the problem, always provide an explicit implementation of the assignment operator for classes that contain dynamically allocated data members, and always ensure that there is a deep copy of those data members.

Vulnerable and fixed code example

1  class A      
2  {
3  public:
4      A(const A &other);
5  };
6  
7  class B
8  {
9  public:
10     B(const B &other);
11     B &operator=(const B &other);
12 };
13 
17 class C
18 {
19 public:
20     void f() {}
21 };
34 };

In this code example, Klocwork flags class A, since it defines a copy constructor and no assignment operator. In contrast, class B defines both a copy constructor and an assignment operator, and doesn't get flagged.