Move Semantics in C++



Move semantics are used in C++ to transfer the ownership of resources from one object to another instead of copying them. It improves the performance as it avoid unnecessary copying of objects, reducing memory usage, improves efficiency, and efficiently handles the temporary objects like rvalue.

Why Move Semantics are Needed?

Before C++11, we used to copy the resource and it was less efficient and created duplicates. In C++11, move semantics were introduced to solve this problem of memory overhead and reducing the duplicates. Here are reasons why we need move semantics −

  • Move semantics avoid copying of resources. It helps in avoiding duplicates and cause less memory overhead.
  • It handles temporary objects like rvalues.
  • It improves the performance by moving the resources instead of copying.

Expression Types in C++

An expression in C++ means any valid combination of variable, operator, constant, and function call that can give result. There are two types of expression which are as follows −

lvalue Expression

An lvalue is a type of expression in which object has a memory address and can be modified if it is not const. Example of lvalue can be a variable, array elements, and many more.

An lvalue reference is used to create a reference to an lvalue. It is denoted by (&) and is used for implementing copy semantics.

rvalue Expression

An rvalue is a type of expression that does not have a memory address and it represents a value that generally appears on the right side. It is a temporary expression that is about to get destroyed. Example of rvalue can be a constant, temporary object, and many more.

An rvalue reference is used to reference rvalues or temporary objects. It is denoted by (&&) and is used for implementing move semantics.

Here is an example of demonstrating lvalue and rvalue in C++. The x is an lvalue expression and x+5 is an rvalue expression.

#include <iostream>
using namespace std;

int main() {
    // x is an lvalue
    int x = 10;  
    // 'x + 5' is an rvalue         
    int y = x + 5;       

    cout << "Old x: " << x << endl;
    cout << "Old y: " << y << endl;

    // Demonstrating assignments
    x = 20;              // correct, as x is an lvalue
    // (x + 5) = 15;     // wrong, as x + 5 is an rvalue

    cout << "New x: " << x << endl;

    return 0;
}

The output of the above code is as follows −

Old x: 10
Old y: 15
New x: 20

The following example demonstrates lvalue reference(int &x) and rvalue reference(int &&x)

#include <iostream>
using namespace std;

void printValue(int &x){
   cout << "Calling with Lvalue reference: " << x << endl; // lvalue reference
}

void printValue(int &&x){
   cout << "Calling with Rvalue reference: " << x << endl; // rvalue reference
}

int main(){
   int a = 10;

   printValue(a);  // a is an lvalue
   printValue(20); // 20 is an rvalue

   return 0;
}

The output of the above code is as follows −

Calling with Lvalue reference: 10
Calling with Rvalue reference: 20

Why do Move Semantics Apply to Rvalues Only?

The move semantics is applied only to rvalues because, the rvalues are temporary objects, that are about to get destroyed. The rvalue will not affect the program in future as they are temporary and can be changed. So, applying move semantics on rvalue to transfer the ownership won't affect the program.

Here are the techniques that can be used to implement move semantics

Move Constructor

A move constructor is a special constructor used to transfer the ownership of resources from a temporary object or rvalue to a new object using rvalue reference. The move constructor is automatically called when initializing an object with an rvalue.

#include <iostream>
#include <cstring>
using namespace std;

class MyString {
   private:
      char *data;
      size_t length;

   public:
   
   // Regular constructor
   MyString(const char *str){
   
      length = strlen(str);
      data = new char[length + 1];
      strcpy(data, str);
      cout << "Constructor called\n";
   }

   // Move constructor 
   MyString(MyString &&other) noexcept {
   
      data = other.data; // transferring ownership
      length = other.length;
      other.data = nullptr; 
      other.length = 0;
      cout << "Move constructor called\n";
   }

   // Destructor
   ~MyString(){
      delete[] data;
      cout << "Destructor called\n";
   }

   void print() const {
      if (data)
         cout << data << endl;
   }
};

// Function returning a temporary object(rvalue)
MyString createString(){
   return MyString("Temporary");
}

int main(){

   // Calling move constructor 
   MyString s1 = createString(); 
   s1.print();

   return 0;
}

The output of the above code is as follows −

Constructor called
Temporary
Destructor called
In the above code, the desired output is not being displayed because of modern compiler apply RVO(Return Value Optimization).

Move Assignment Operator

The move assignment operator uses '(=)' and rvalue reference for move semantic. First, it releases the current resource of object and then take ownership of the source object's resources. Below is an example of move assignment operator −

#include <iostream>
#include <cstring>
using namespace std;

class MyString {
   private:
      char* data;
      size_t length;

   public:
      // Regular constructor
      MyString(const char* str) {
         length = strlen(str);
         data = new char[length + 1];
         strcpy(data, str);
         cout << "Constructor called\n";
      }

      // Move constructor
      MyString(MyString&& other) noexcept {
         data = other.data;
         length = other.length;
         other.data = nullptr;
         other.length = 0;
         cout << "Move constructor called\n";
      }

      // Move assignment operator
      MyString& operator=(MyString&& other) noexcept {
         if (this != &other) {
            delete[] data;          
            data = other.data;      // transferring ownership
            length = other.length;
            other.data = nullptr;   
            other.length = 0;
            cout << "Move assignment operator called\n";
         }
         return *this;
      }

      // Destructor
      ~MyString() {
         delete[] data;
         cout << "Destructor called\n";
      }

   void print() const {
      if (data)
        cout << data << endl;
   }
};

int main() {
   MyString s1("Hello");
   MyString s2("World");

   cout << "Before move assignment:\n";
   s1.print();
   s2.print();

   s1 = std::move(s2); // Move assignment operator called

   cout << "After move assignment:\n";
   s1.print();
   s2.print(); // s2 is now empty 

   return 0;
}

The output of the above code is as follows −

Constructor called
Constructor called
Before move assignment:
Hello
World
Move assignment operator called
After move assignment:
World
Destructor called
Destructor called

std::move() Function

Below is an example to transfer the ownership using the std::move() function

#include <iostream>
#include <string>
#include <utility> // for move
using namespace std;

int main(){
   cout << "Before move() function:";
   string str1 = "Hello";
   cout << "\nstring 1: " << str1 << endl;

   cout << "\nAfter move() function:";
   string str2 = move(str1);
   cout << "\nstr2: " << str2;
   cout << "\nstr1: " << str1 << "\n";

   return 0;
}

The output of the above code is as follows −

Before move() function:
string 1: Hello

After move() function:
str2: Hello
str1: 

Conclusion

In this chapter, we discussed about move semantics. Its main purpose is to transfer the ownership of resources of rvalues to save memory overhead and increase the efficiency of the code.

Advertisements