"memcpy", “memmove”, and “memset” should only be called with pointers to trivially copyable types
A class type is trivially copyable if:
One or more of the following special member functions is trivial and
the rest are deleted: copy constructor, move constructor, copy
assignment operator, and move assignment operator, It has a trivial,
non-deleted destructor, It has trivially copyable members and base
classes, It has no virtual functions. Note: a default implementation,
both explicit (with =default) or implicit (if the special member
function is omitted), is considered trivial.
Noncompliant Code Example
class Shape {
public:
int x;
int y;
virtual ~Shape(); // This makes the class non trivially copyable
};
void f(Shape *dest, Shape *source)
{
memcpy(dest, source, sizeof Shape); // Noncompliant
}
Compliant Solution
class Shape {
public:
int x;
int y;
virtual ~Shape(); // This makes the class non trivially copyable
};
void f(Shape *dest, Shape *source)
{
(*dest) = (*source);
}