Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Why are Move operations more efficient than Copy operations?

  1. Apr 4, 2017 #1
    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?
  2. jcsd
  3. Apr 4, 2017 #2


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted