I know that C++ move semantics is supposed to save processor power and memory since
Move::Move(Shallow&& source) noexcept // Move constructor
{
data = source.data; // Assume data is an array of size = size and all initiated to a user specific value
size = source.size;
source.size =0;
source.data ={nullptr};
}
supposing that all array indexes are initialized to a specific variable, so that the move semantic will just save array pointer in memory and nulls out the the source array, like in the example above, This will prevent a new array to be created dynamically if we use a copy constructor (Deep copy constructor to be specific ) But
1) Is there any benefit using move constructor if we assume that data is just a simple integer or even a very large uninitialized array 2) This move constructor seems pretty much the same as just calling copy constructor with a shallow copying like
Copy::Copy(const &source){ // A shallow copy
data = source.data; // Assume data is an array of size = size
size = source.size;
}
The only difference of course is Nulling data , and size in move construct , so is there any performance improvement between Both pieces of code above in terms of memory and performance, since Nulling the data pointer and size doesn't actually save us space or memory right, or i miss something here.
This question so that i can know when to use shallow copying or Move semantics and if there is any difference between them (except for nulling down of attributes in the move way).
Is there any benifet using move constructor if we assume that data is just a simple integer or even a very large uninitialized array
No.
This move constructor seems pretty much the same as just calling copy constructor with a shallow copying.
It is.
"Moving" only works to transfer ownership of resources, and you can only do that if the resources are held indirectly, such as via a pointer. Then you can swap the pointer. But if you have to swap the actual data, then that's basically just a copy.
Most standard containers (such as std::vector
, and excepting std::array
) hold their data indirectly, behind a pointer, so these have useful move semantics.