Report an Error

Reported page · Common Mistake

Confusing Shallow Copy and Deep Copy

/mistakes/computer-science-confusing-shallow-copy-and-deep-copy/

or drop them here

Up to 3 images or PDFs, 5 MB per file. 0 of 3 attached.

Save resource to class

Loading your classrooms…

Computer Science

Confusing Shallow Copy and Deep Copy

A student writes b = a.copy() and expects changes inside b's nested objects to leave a unchanged.

Quick check

Why students make it

The misleading intuition is that copying a container also copies everything it points to. In many languages, a copy operation only duplicates one level unless a deep-copy operation is requested.

The rule done right

A shallow copy creates a new outer container but keeps references to the same nested objects. A deep copy recursively copies nested objects so independent mutation of nested state does not affect the original.