R copy semantics

imageIn the we looked at the issues of resource management in C++ and introduced “The Rule of The Big Three (and a half)”. In this article we’ll extend this concept by looking at the idea of move semantics, a feature introduced in C++11. Move semantics mean we’ll have to extend our rule to “The Rule of The Big Five” or, perhaps more correctly, “The Rule of The Big Four (and a half)”.

The cost of copying

Earlier, we had defined a SocketManager class that manages the lifetime of a Socket object. Here’s the type definition for the SocketManager:

The Socket is allocated in the SocketManager constructor; and de-allocated in the destructor.

imageThe SocketManager class implements both the copy constructor and assignment operator, providing a ‘deep copy’ policy. You should read the previous article if you’re not already familiar with the Copy-Swap Idiom I’ve used here.

Copying objects may not be the best solution in many situations. It can be very expensive in terms of creating, copying and then destroying temporary objects. For example:

  • Returning objects from functions
  • Some algorithms (for example, swap)
  • Dynamically-allocating containers.

Let’s take a vector of SocketManagers as an example:

As expected we get the constructor for the (temporary) SocketManager object, then the copy constructor as it is inserted into the vector. The second copy constructor is caused by the vector memory allocator creating space for two new objects then copying the objects across. This is an expensive operation since every copy requires de-allocation / re-allocation of memory and copying of contents.

Here we’re using the default constructor for the SocketManager class. We will discuss the consequences of this later.

For this example I’m deliberately ignoring emplacement of the objects; although the copying will still occur as memory is allocated.

image image

Traffic stats
DEMDACO - Home Willow Tree Inquisitive Child
Home (DEMDACO - Home)
  • Nurtured by your loving care
  • Figurine is 3 tall
  • Artist Susan Lordi hand carves the original of each Willow Tree figurine from her art studio on Kansas City, Missouri
  • Willow Tree is an intimate line of figurative sculptures that speak in quiet ways to heal, comfort, protect and inspire
  • Arrives in a gift box, ready for gift giving with an enclosure card
Unit Bricks 72 Piece Mini Unit Bricks Architect Set - (Wood Building / Stacking Blocks - Toy for Kids & Children 3 Years & Up)
Toy (Unit Bricks)
  • 3-4 year olds are detail-oriented, capable of matching and grouping items by shape and color. Just starting to exercise their imaginations with pretend play, at...
  • 5-6 year olds are investigators, able to hypothesize about their surroundings and test theories in play situations. They take pride in the final product of their...
  • 7-8 year olds have a stronger sense of self and are more comfortable taking risks with hands-on experimentation. They can engage in complex problem solving and communicate...
  • Are you looking for an educational toy for your kids or even someone else s children to enjoy? Look no further, Unit Bricks makes an excellent gift for boys or...
  • A brick is the basic unit of most building foundations. It s also the basic unit of key learning foundations. Simple unit brick play empowers kids and ignites their...
Bodega on Madison iPhone 6 Ouija Board Case (TPU), for the Spiritually Inquisitive Individuals!
Wireless (Bodega on Madison)
  • To insure purchase of genuine products, please make sure to check out using Bodega on Madison s add to cart.
  • Designed, assembled, shipped from the USA (not from China) exclusively by Bodega on Madison.
  • Glove-like fit that firmly locks into place and minimizes collection of debris between mating surfaces.
  • Shock resistant rubber-like plastic polymer (NOT HARD PLASTIC) is smooth, yet flexible. Does not stretch or loses its shape after prolonged use. Does not cling to...
  • Allows full access to all exterior functional components
FSKcase? Inquisitive Octopus iPhone 6 Plus Shadow green Tough Phone Case
Wireless (FSKcase?)
  • Protect and style your Phone case with this Solid Protector case
  • New and High grade Hard Shell quality
  • Lightweight, semi-rigid protection
  • Smart design make access to all buttons be so easy to use
  • You can provide me the picture you like ,i will print it on the case .Above 800*800 PIX
beingTRUE being TRUE - Satin Lip Shine - Inquisitive
Beauty (beingTRUE)
  • beingTRUE
  • Satin Lip Shine
  • Modern color meets sophisticated shine.
  • Inquisitive
Related Posts