0

My computer science professor's question posed an interesting question that I was curious what the answer to it was.

Your copy assignment operator makes a shallow copy of an array. Why is this problematic? Provide a scenario when this would lead your program to fault.

First of all, I know that copy assignment operators are used to copy over one object's data to another one, usually using a deep copy, but I am not exactly sure what the problem would be with using a shallow copy.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Hint: What happens if the array is allocated on the heap? – Brady Dean Oct 08 '21 at 02:41
  • 1
    It sounds like your professor doesn't understand [Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). Shallow copies aren't a problem if your destructor understands them. – Silvio Mayolo Oct 08 '21 at 02:42
  • [healthy discussion in this SO question regarding deep Vs shallow](https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy) – dorKKnight Oct 08 '21 at 03:02
  • Working from Silvio Mayolo's comment, you need to determine [who owns the array?](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) – user4581301 Oct 08 '21 at 03:10
  • The question requires more context, as it assumes something about your class. (Perhaps this question was directed at a specific class, not as a general query?) A shallow copy by itself is insufficient to cause a fault. For a simple example, suppose you are willing to leak memory. If your destructor does not release resources, a shallow copy will not cause a fault in any scenario. Something more is needed, a shallow copy plus something else in your implementation. – JaMiT Oct 08 '21 at 03:37

1 Answers1

2

The professor's question asks for a scenario where a shallow copy of an array causes a fault. A violation of the Rule of 3/5/0 is such a scenerio.

In short, the Rule of 3 states that if a class implements a copy constructor, a copy assignment operator, or a destructor, it likely needs all 3. The Rule of 5 extends this to include the move constructor and move assignment operator.

In a deep copy scenario, each object would own its own array in memory. The copy constructor and copy assignment operator would make a full copy of the source object's current array. The destructor would free that copy. Any modifications made by an object to its own array would not affect any other objects.

In a shallow copy scenario, each object would share pointers to a single array in memory. The copy constructor and copy assignment operator would copy the source object's pointer to the array. Modifications to the array would affect all objects pointing at that array. The problem is when there is no clear definition for which object owns the array. If all of those objects think they own the array, theor destructors will all try to free the same memory multiple times, which is undefined behavior that can corrupt memory, cause crashes, etc. But if none of them assume ownership, the array will be leaked once they have all been destroyed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770