Difference between Copy constructor vs Move constructor

 

Copy ConstructorsMove Constructors
Copy constructors takes lvalue reference as an argument.Move constructors takes rvalue reference as argument.
Copy constructors create a new object from passed object by copying each and every item into a new memory location.Move constructors create a new object by using as much memory from passed object.
Since, copy constructors use a lot of new memory allocation(for new object). Hence, copy constructors gives bad performance compared to move constructorsSince, move constructors use most of memory blocks from passed object. Hence, move constructors gave better performance compared to copy constructors.
Since, copy constructors doesn’t make any change to passed object. Hence, passed object can be used after copy operations also.Since, move constructors utilizes memory blocks from passed object. Hence, passed object can’t be used after move operations.

For more information related to Move constructors click here

Difference between Copy constructor vs Move constructor Read More

Move Assignment Operator explained with simple example

In C++11 (Introduction to C++11) , move assignment operator are added which can improve any code’s performance drastically. Typically a move assignment operator is same as copy/move constructor but before reusing the memory blocks from passed object, it releases all the memory which is owned by this object. Since, memory reallocation is avoided in case of move assignment operators. Thus it gives better performance.

However while implementing move assignment operators we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move assignment.

(more…)

Move Assignment Operator explained with simple example Read More

std::move explained with simple example

std::move is a C++11(Introduction to C++11) utility function which is used to convert an lvalue to rvalue expression to forcefully call the move constructor or move assignment operator which will improve the program’s performance drastically. This utility function is only change lvalue into rvalue expression that’s it. This function doesn’t do any kind of memory movement.

Implementation of this function is as follows:

From C++11 onwards:

template< class T >
typename std::remove_reference<T>::type&& move( T&& t );

From C++14 onwards:

template< class T >
constexpr typename std::remove_reference<T>::type&& move( T&& t );

Let’s take an example of usage of this move function:

(more…)
std::move explained with simple example Read More

Move Constructors explained with simple example


In C++11 (Introduction to C++11) , move constructors are added which can improve any code’s performance drastically. Typically a move constructor is same as copy constructor which will create a new instance based on the passed object. But, the idea behind move constructor is to avoid memory reallocation and use as many memory from the passed original object because the original object is about to be deleted as it has been provided as a temporary object.

Since, in move constructors we avoid memory reallocation while creating new instance while will improve the code’s performance as we know memory allocation is quite costly.
While implementing move constructors we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move constructor.

(more…)

Move Constructors explained with simple example Read More

Rvalue References


Before understanding about this new feature “Rvalue references” of C++11(Introduction to C++11), we should first understand what is lvalue and rvalue in C++.

Typically Lvalue is an expression which returns a permanent memory address which can be assigned to any variables. For ex:

int x = 10;
int y;
y = 20;

Here both x and y are lvalues. In some cases, we can have functions also as lvalue as shown in below example:

int x;  // global variable

// Returning reference to global variable x
int& getXRef()
{
    return x;
}

getXRef() = 30;

Now this function can act as lvalue too as shown above.

(more…)

Rvalue References Read More