Why are Move operations more efficient than Copy operations?

  • Context:
  • Thread starter Thread starter MinusTheBear
  • Start date Start date
  • Tags Tags
    Operations
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 1K views
MinusTheBear
Messages
22
Reaction score
0
Hey all,

Posting once again. I'm only in my second class on programming ever (with no prior experience to classes). I'm learning about move/copy operations. My textbook says that the move operations is "obviously more efficient than the copy" operation, but it doesn't explain why.

Is it more efficient in the case of deep and shallow copies? The only thing I can really think of is if I had an extremely large array or class object that "stealing" the objects data would be more efficient rather than having two copies of the same object with different memory locations. But at the same time, I don't really see why this is so important because if the array or class object is temporary, it's going to be deleted once it falls out of scope anyway -- so it really just seems like another form of copying to me. I know that the idea is you're moving an r-value to an l-value, but the r-value is temporary, so I don't see why you couldn't just perform a copy with a r-value reference since once the copy is performed, the r-value will fall out of scope and be deleted.

Am I missing something?
 
Physics news on Phys.org
Two differences come to mind, one is efficiency and the other is a practical data maintenance issue. I don't know if either of these are what your book had in mind.
1) Often a move of a lot of data only requires that a pointer to the data be moved. The actual data is not moved.
2) Duplicating data causes data maintenance and consistency nightmares. Too much copying often leads you to searching all over the place if the data needs to be changed.
 
  • Like
Likes   Reactions: Of Mike and Men and berkeman