Amazon Interview Question
Software Engineer / DevelopersHardCopy is copying the values from one structure to another.If there are pointers in the structure, then the second structure also points to the same pointer location as in the first one.
Shallow copy is copying bit by bit, If there are pointers in the structure to be copied,then a new memory location is created and data is copied to that location.
A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies.
- creation December 09, 2009A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disasterous consequences.