Shallow Copy vs Deep Copy (Part I)

Опубликовано: 17 Май 2026
на канале: Brian MalloyOn
4,728
25

This video illustrates the dangers inherent in using the default copy constructor by writing a string program that generates a double free error. The error results because the compiler generated copy constructor is a shallow copy. We fix the error by writing our own copy constructor that implements a deep copy, thus eliminating the double free error. We then induce the error again by using the compiler generated assignment operator. In the next video we fix the error induced by the compiler generated assignment operator by writing our own assignment operator that implements a deep copy. We also illustrate that this same problem exists in the Python language.