C++ memcpy do not copy the class

The one who recently took over the predecessor has a set of C++ code, first explain that the predecessor was in C before. After taking over, due to changes in customer requirements, some codes were adjusted; however, abnormal manifestations occurred.
Every operation is normal, but after the program runs, double free corruption will appear;
from the surface analysis, it is caused by the release of a certain block of memory twice; through Debug debugging, it is releasing a certain memory. A member variable in an object appears; later, by looking at the code, it is found that memcpy is used for the assignment of the class variable; resulting in a release error.

The root cause : memcpy is viewed from the perspective of C++. This operation is a simple shallow copy, not a deep copy; that is, copying a pointer is only to obtain the data address pointed to by it. Therefore, the content pointed to by the pointer in the class cannot be directly copied by memcpy.

Modification method : use copy constructor and overload operator method.

Guess you like

Origin blog.csdn.net/CFH1021/article/details/105072624