What are the differences between shallow and deep copies?

  • Thread starter Chromium
  • Start date
In summary: A shallow copy is just a copy of the object reference, which means that if you modify the source object, the shallow copy will still reflect the changes. A deep copy, on the other hand, is a copy of the object itself. This means that if you modify the deep copy, the changes will be reflected in the original source object as well.
  • #1
Chromium
56
0
Hey Everyone,

Could someone confirm whether or not this "picture" of a shallow and deep copy is accurate? Since I've only programmed in Java, my code/pseudo-code will be java-ish.

Shallow Copy

Object Reference String Object
String s 10010101010

In this case, a String object has a String reference s "pointing" to it.


Object Reference String Object
String s 10010101010
t

In this case, the same String object has two object references pointing to it. In
actuality, t is just a copy of s. So in short, a shallow copy is just a copy of the
object reference. Ultimately, you have two object references pointing to the
same object.

Deep Copy

Object Reference String Object
String s 10010101010

The String object still only has reference pointing to it.

Object Reference String Object
String s 10010101010
String t 10010101010

Now there is a completely new and different String object (however, it is a copy
of the original String object. The new object reference points to a completely
distinct (yet exactly alike) object.



Feel free to criticize this "picture" of shallow v. deep copies if it is vague, unclear,
plain wrong, etc...(but please be gentle!)
 
Last edited:
Technology news on Phys.org
  • #2
Your table formatting confuses me a little but otherwise yes, that is exactly correct.
 
  • #3
Sorry about that, I'm not sure why it turned out like that. I went back to edit it, but the tables are still the same, oh well. By the way, thanks for reassuring me that I indeed understand deep and shallow copies!
 
  • #4
Chromium said:
Sorry about that, I'm not sure why it turned out like that. I went back to edit it, but the tables are still the same, oh well. By the way, thanks for reassuring me that I indeed understand deep and shallow copies!

Wrap your text in [ CODE ] tags:

Code:
[B]Object   Reference[/B] 
String   s          10010101010
 
  • #5
Yes, or put another more useful way - shallow copies can be overwritten by modifying the source object, deep copies are not changed by modifying the original source object.
Shallow copies allow you to shoot yourself in the foot more easily, in other words.
 

What is the difference between deep copy and shallow copy?

Deep copy and shallow copy are two different methods of copying objects or data structures in computer programming. A deep copy creates an entirely new object with its own unique memory address, while a shallow copy creates a new object that references the same memory locations as the original object.

When should I use a deep copy?

You should use a deep copy when you want to create a new and independent copy of an object or data structure. This is useful when you need to modify the copied object without affecting the original, or when the original object is no longer needed and can be safely deleted.

When should I use a shallow copy?

A shallow copy is useful when you want to create a new object that shares some or all of its data with the original object. This can be more efficient in terms of memory usage, but it also means that changes made to the copied object will also affect the original object.

What is the default copy method in most programming languages?

The default copy method in most programming languages is a shallow copy. This means that when you assign a variable to another variable, the two variables will reference the same object in memory.

Can I choose which type of copy to use in my code?

Yes, most programming languages allow you to choose between deep copy and shallow copy methods. You can do this by using specific functions or methods designed for each type of copy, or by implementing your own custom copy method for a particular object or data structure.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
1
Views
971
  • Programming and Computer Science
Replies
7
Views
10K
  • Beyond the Standard Models
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
894
  • Programming and Computer Science
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
17
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Introductory Physics Homework Help
Replies
19
Views
3K
Back
Top