I wanted to know what exactly happens internally when we call std::move on an object.
For example:
class Holder {
int* m_ptr;
int m_size;
public:
Holder(int size) : m_size(size),
m_ptr(size ? new int[size] : 0) {
cout << "Constructor\n";
}
~Holder() {
cout << "Destructor\n";
delete[] m_ptr;
}
Holder(const Holder& other) {
cout << "Copy Constructor\n";
m_ptr = new int[other.m_size];
std::copy(other.m_ptr, other.m_ptr + other.m_size, m_ptr);
m_size = other.m_size;
}
void swap(Holder& other) noexcept {
std::swap(this->m_ptr, other.m_ptr);
std::swap(this->m_size, other.m_size);
}
Holder& operator=(const Holder& other) {
cout << "Copy Assignment\n";
Holder(other).swap(*this);
}
Holder(Holder&& other) : m_ptr(other.m_ptr), m_size(other.m_size) {
cout << "Move Constructor\n";
other.m_ptr = nullptr;
other.m_size = 0;
}
Holder& operator=(Holder&& that) noexcept {
cout << "Move Assignment\n";
Holder(std::move(that)).swap(*this);
return *this;
}
};
Holder createHolder(int size) {
return Holder(size);
}
int main(void) {
Holder h = createHolder(1000);
return 0;
}
Gives the output as when compiled with '-fno-elide-constructors':
Constructor
Move Constructor
Destructor
Move Constructor
Destructor
Destructor
Since we are returning from a function createHolder and the return object is on the stack, how does the main function still receives it? Not getting exactly what happens internally in memory.
Thanks.
std::move is a noop function that converts any kind of reference into an rvalue reference. So it doesn't actually "do" anything, it just changes how it's argument is used.
template <class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;
Returns: static_cast<remove_reference_t<T>&&>(t).
std::move make your operator =, constructor is call correctly with what you defined in the Holder class
Holder(Holder&& other) : m_ptr(other.m_ptr), m_size(other.m_size) {
cout << "Move Constructor\n";
other.m_ptr = nullptr;
other.m_size = 0;
}
Holder& operator=(Holder&& that) noexcept {
cout << "Move Assignment\n";
Holder(std::move(that)).swap(*this);
return *this;
}
Since we are returning from a function createHolder and the return object is on the stack, how does the main function still receives it?
because your move constructor
you already copy pointer m_ptr(other.m_ptr) m_size(other.m_size) from
Holder(size) -> move to Holder object in return register on stack -> move to Holder h in main function
Constructor -> of Holder(size);
Move Constructor -> Holder(size) move to Holder object in return register on stack
Destructor -> destructor of Holder(size)
Move Constructor -> from Holder object on stack -> Holder h in main
Destructor -> destructor of Holder object in return register
Destructor -> destructor of Holder h in main
Related
I'm trying to make a sample implementation of std::unique_ptr. Here is the code:
include <iostream>
template<typename T>
class UniquePtr
{
public:
explicit UniquePtr(T * ptr)
:m_ptr(ptr)
{};
UniquePtr(UniquePtr const & other) = delete;
UniquePtr& operator=(UniquePtr const & other) = delete;
explicit UniquePtr(UniquePtr && other)
{
m_ptr = other.m_ptr;
other.m_ptr = nullptr;
}
UniquePtr & operator=(UniquePtr && other)
{
std::cout << "Move assignment called " << std::endl;
m_ptr = other.m_ptr;
other.m_ptr = nullptr;
}
~UniquePtr()
{
delete m_ptr;
}
T& operator*()
{
return *m_ptr;
}
T& operator->()
{
return m_ptr;
}
private:
T * m_ptr = nullptr;
};
int main()
{
UniquePtr<int> t(new int(3));
t= UniquePtr<int>(new int(4));
std::cout << *t << std::endl;
}
This code compiles and I'm able to see the value 4 in the output even after deleting the default assignment and copy constructor. What am I doing wrong?
In the assignment, move is called because the UniquePtr<int>(new int(4)) is constructing a temporary object, and in this case the compiler tries to use the move assignment if possible, else it would fall back to the copy assignment, which is deleted.
UniquePtr<int> t(new int(3));
t= UniquePtr<int>(new int(4)); // move assignment because temporary
auto k = t; // copy assignment since t is not temporary and so does not compile.
As commented, the move assignment is not returning *this, you should enable all warnings. Also the last operator-> has a syntax error, it returns a pointer but expects a reference.
Additionally, your code has a major issue, in case of exceptions you could have a memory leak. Suppose you write something like:
class Banana
{ ... }
void eatBanana(UniquePtr<Banana> banana, int amountToEat);
int computeAmount();
eatBanana(UniquePtr<Banana>(new Banana(3)), computeAmount());
If, for any reason, the computeAmount() function throws an exception, then the memory allocated by new could never be released, because computeAmount() could be executed between new and the constructor of the UniquePtr. For this reason, we normally use std::make_unique().
You should implement your own version of make_unique() and use it, it's trival, see here: https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
Here more information about the issues and the solution:
https://www.oreilly.com/library/view/effective-modern-c/9781491908419/ch04.html
Hello! I am making my own string class named strings
On passing a R-value move constructor and move assignment operator are being called
This is the main function, all i am doing is passing R-value ref, but while debugging only copy are or both move and copy.
int main()
{
strings s1="hey!";
cout<<"\n";
s1=strings("bye");
cout<<"\n";
s1="Hello";
cout<<"\n";
strings s2= strings{"Rhythm"};
s2.print();
return 0;
}
//move comstructor
strings::strings(strings &&obj):s{obj.s}
{
std::cout<<"Calling move const\n";
obj.s=nullptr;
}
//move assinment operator
strings& strings::operator=(strings &&obj)
{
std::cout<<"Using move\n";
if(this==&obj){
return *this;
}
delete[] s;
s=obj.s;
obj.s=nullptr;
return *this;
}
//copy constructor;
strings::strings(const strings &obj)
{
s=new char[strlen(obj.s)+1];
strcpy(this->s,obj.s);
}
An moving assignment (not an assignment initialization) requires a move operator=(strings&&). It is not available if you defined own copy constructor. Note, that in situation like this
struct S {
S() {}
S(const S &obj) { std::cout << "Copy ctor" << std::endl;}
S(S &&obj) { std::cout << "Move ctor" << std::endl;}
S& operator=(const S &obj) { std::cout << "Copy =" << std::endl; return *this;}
S& operator=(S &&obj) { std::cout << "Move =" << std::endl; return *this;}
};
int main()
{
S a = S();
a = S();
}
with C++11 compiler the declaration line wouldn't even produce output, because no copy or move did happen, it was elided.
In the following example, I am trying if I try to return by reference from fun() then it gives me warning as it should.
However when I try to return by value, I am expecting 'operator=()' to get triggered, but it is throwing the following error which I am not able to figure out why:
error: no match for ‘operator=’ (operand types are ‘Auto_ptr2<Resource>’ and ‘Auto_ptr2<Resource>’)
note: no known conversion for argument 1 from ‘Auto_ptr2<Resource>’ to
‘Auto_ptr2<Resource>&’
Surprisingly, res2=res1 invoked 'operator=()' just fine!!!
template<class T>
class Auto_ptr2
{
T* m_ptr;
public:
Auto_ptr2(T* ptr=nullptr)
:m_ptr(ptr)
{std::cout<<"\nAuto_ptr CTOR called ";}
~Auto_ptr2()
{
std::cout<<"\n~Auto_ptr2 called\n";
delete m_ptr;
}
// A copy constructor that implements move semantics
Auto_ptr2(Auto_ptr2&
{
std::cout<<"\nAuto_PTR2 copy constructor called";
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
}
Auto_ptr2& operator=(Auto_ptr2& a)
{
std::cout<<"\nAuto_ptr2 operator = called";
if (&a == this)
return *this;
delete m_ptr;
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
return *this;
}
T& operator*() const { return *m_ptr; }
T* operator->() const { return m_ptr; }
bool isNull() const { return m_ptr == nullptr; }
};
class Resource
{
public:
Resource() { std::cout << "\nResource acquired"; }
~Resource() { std::cout << "\nResource destroyed"; }
};
Auto_ptr2<Resource> fun(Auto_ptr2<Resource> res3)
{
std::cout << "\nres1 is " << (res3.isNull() ? "null" : "not null");
return res3;
}
int main()
{
Auto_ptr2<Resource> res1(new Resource);
Auto_ptr2<Resource> res2;
res2 = res1; // res2 assumes ownership, res1 is set to null
res2=fun(res2); //ERROR : error: no match for ‘operator=’ (operand types are ‘Auto_ptr2<Resource>’ and ‘Auto_ptr2<Resource>’)
return 0;
}
If I understand correctly, you're writing something similar to std::unique_ptr.
So, as for std::unique_ptr, the operator=() should works with move semantics, so receiving a r-vale reference, Auto_ptr &&, not an l-value reference
Auto_ptr2& operator=(Auto_ptr2 && a)
{
std::cout<<"\nAuto_ptr2 operator = called";
if (&a == this)
return *this;
delete m_ptr;
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
return *this;
}
And the use should pass through std::move()
res2=fun(std::move(res2));
Same problem with constructors: avoid copy constructor (maybe delete it) and write a move constructor.
Auto_ptr2 (Auto_ptr2 const &) = delete;
Auto_ptr2 (Auto_ptr2 && a)
{
std::cout<<"\nAuto_PTR2 move constructor called";
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
}
despite the ocean of smart pointer questions out there, I seem to be stuck with one more. I am trying to implement a ref counted smart pointer, but when I try it in the following case, the ref count is wrong. The comments are what I think should be the correct ref counts.
Sptr<B> bp1(new B); // obj1: ref count = 1
Sptr<B> bp2 = bp1; // obj1: ref count = 2
bp2 = new B; // obj1: ref count = 1, obj2: rec count = 1 **problem**
In my implementation, my obj2 ref count is 2, because of this code:
protected:
void retain() {
++(*_rc);
std::cout << "retained, rc: " << *_rc << std::endl;
}
void release() {
--(*_rc);
std::cout << "released, rc: " << *_rc << std::endl;
if (*_rc == 0) {
std::cout << "rc = 0, deleting obj" << std::endl;
delete _ptr;
_ptr = 0;
delete _rc;
_rc = 0;
}
}
private:
T *_ptr;
int *_rc;
// Delegate private copy constructor
template <typename U>
Sptr(const Sptr<U> *p) : _ptr(p->get()), _rc(p->rc()) {
if (p->get() != 0) retain();
}
// Delegate private assignment operator
template <typename U>
Sptr<T> &operator=(const Sptr<U> *p) {
if (_ptr != 0) release();
_ptr = p->get();
_rc = p->rc();
if (_ptr != 0) retain();
return *this;
}
public:
Sptr() : _ptr(0) {}
template <typename U>
Sptr(U *p) : _ptr(p) { _rc = new int(1); }
// Normal and template copy constructors both delegate to private
Sptr(const Sptr &o) : Sptr(&o) {
std::cout << "non-templated copy ctor" << std::endl;
}
template <typename U>
Sptr(const Sptr<U> &o) : Sptr(&o) {
std::cout << "templated copy ctor" << std::endl;
}
// Normal and template assignment operator
Sptr &operator=(const Sptr &o) {
std::cout << "non-templated assignment operator" << std::endl;
return operator=(&o);
}
template <typename U>
Sptr<T> &operator=(const Sptr<U> &o) {
std::cout << "templated assignment operator" << std::endl;
return operator=(&o);
}
// Assignment operator for assigning to void or 0
void operator=(int) {
if (_ptr != 0) release();
_ptr = 0;
_rc = 0;
}
The constructor is initialized with a ref count = 1, but in my assignment operator, I am retaining the object, making the ref count = 2. But it should only be 1 in this case, because bp2 = new B is only one pointer to that object. I've looked over various smart pointer implementation examples, and I can't seem to figure out how they deal with this case that I'm having trouble with.
Thanks for your time!
The assignment operator is riddled with small errors and unnecessary complex anyway. For example, when you assign Sptr<T> to itself it will have funny effects. Most manually written assignment operators should look like this:
T& T::operator= (T const& other) {
T(other).swap(*this);
return *this;
}
... or
T& T::operator= (T other) {
other.swap(*this);
return *this;
}
Once stateful allocators enter the game things change a bit but we can ignore this detail here. The main idea is to leverage the existing work done for the copy constructor and the destructor. Note, that this approach also works if the right hand side isn't T, i.e., you can still leverage a corresponding constructor, the destructor, and swap(). The only potentially additional work is desirable anyway, and trivial to implement: the swap() member. In your case that is very simple, too:
template <typename T>
void Sptr<T>::swap(Sptr<T>& other) {
std::swap(this->_ptr, other._ptr);
std::swap(this->_rc, other._rc);
}
Just a note on your assignment operator taking an int: This is a very bad idea! To reset the pointer, you should probably better have a reset() method. In C++ 2011 you could reasonably have a method taking a std::nullptr_t for this purpose, though.
Define an assignment operator for the raw pointer type. Otherwise, it will construct a new smart pointer with ref count 1, which you will then increase to 2.
Example:
template <typename U>
Sptr<T> &operator=(U *p)
{
if (_ptr != 0)
{
_ptr = p;
_rc = new int(1);
}
return *this;
}
This stuff is notoriously tricky, and there are probably more bugs waiting. I would make the constructor explicit to prevent other unintentional constructions.
From what I see of your code, you need a proper destructor in your smart pointer class to call release and fix the counter. You need at least that modification for your counter to work correctly.
I didn't see a Sptr(T *) constructor in your code, did you omit it?
As a side note, I would probably use a std::pair to store the counter and the T pointer, but your data layout works as it is. As another answer pointed it out, you should pay attention to the self assignment case though.
Here's a minimal implementation following your data layout and interface choices:
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class Sptr {
protected:
T *_ptr;
int *_rc;
virtual void retain() {
if (_rc) // pointing to something
++(*_rc);
clog << "retain : " << *_rc << endl;
}
virtual void release() {
if (_rc) {
--(*_rc);
clog << "release : " << *_rc << endl;
if (*_rc == 0) {
delete _ptr;
_ptr = NULL;
delete _rc;
_rc = NULL;
}
}
}
public:
Sptr() : _ptr(NULL), _rc(NULL) {} // no reference
virtual ~Sptr() { release(); } // drop the reference held by this
Sptr(T *p): _ptr(p) { // new independent pointer
_rc = new int(0);
retain();
}
virtual Sptr<T> &operator=(T *p) {
release();
_ptr = p;
_rc = new int(0);
retain();
return *this;
}
Sptr(Sptr<T> &o) : _ptr(o._ptr), _rc(o._rc) {
retain();
}
virtual Sptr<T> &operator=(Sptr<T> &o) {
if (_rc != o._rc){ // different shared pointer
release();
_ptr = o._ptr;
_rc = o._rc;
retain();
}
return *this;
}
};
int main(){
int *i = new int(5);
Sptr<int> sptr1(i);
Sptr<int> sptr2(i);
Sptr<int> sptr3;
sptr1 = sptr1;
sptr2 = sptr1;
sptr3 = sptr1;
}
_rc is the indicator in your class that a pointer is shared with another instance or not.
For instance, in this code:
B *b = new B;
Sptr<B> sptr1(b);
Sptr<B> sptr2(b);
sptr1 and sptr2 are not sharing the pointer, because assignments were done separately, and thus the _rc should be different, which is effectively the case in my small implementation.
I am new in move constructor, I surveyed from some sites and tried using the Visual Studio 11 Express Beta..
Below is my testing code...
#include <iostream>
using namespace std;
class Foo
{
public:
Foo()
: Memory(nullptr)
{
cout<<"Foo Constructor"<<endl;
}
~Foo()
{
cout<<"~Foo Destructor"<<endl;
if(Memory != nullptr)
delete []Memory;
}
Foo(Foo& rhs)
: Memory(nullptr)
{
cout<<"Copy Constructor"<<endl;
//allocate
//this->Memory = new ....
//copy
//memcpy(this->Memory, rhs.Memory...);
}
Foo& operator=(Foo& rhs)
{
cout<<"="<<endl;
}
void* Memory;
Foo(int nBytes) { Memory = new char[nBytes]; }
Foo(Foo&& rhs)
{
cout<<"Foo Move Constructor"<<endl;
Memory = rhs.Memory;
rhs.Memory = nullptr;
}
};
Foo Get()
{
Foo f;
return f;
//return Foo();
}
void Set(Foo rhs)
{
Foo obj(rhs);
}
int main()
{
Set(Get());
return 0;
}
I don't know why it will not enter move constructor.
It's really a Rvalue from Get();
If I modified non-const copy constructor from const constructor,
it will enter move constructor. Behavior changed...
Could anyone kindly explain why it happened?
#include <iostream>
using namespace std;
class Foo
{
public:
Foo():
Memory(nullptr)
{
cout<< this << "Foo Constructor"<<endl;
}
~Foo()
{
cout<< this << "~Foo Destructor"<<endl;
if(Memory != nullptr)
delete []Memory;
}
Foo(Foo& rhs)
:Memory(nullptr)
{
cout<<this << "Copy Constructor"<<endl;
//allocate
//this->Memory = new ....
//copy
//memcpy(this->Memory, rhs.Memory...);
}
Foo& operator=(Foo& rhs)
{
cout<<"="<<endl;
}
void* Memory;
Foo(int nBytes) { Memory = new char[nBytes]; }
Foo(Foo&& rhs)
{
cout<<this << "Foo Move Constructor"<<endl;
Memory = rhs.Memory;
rhs.Memory = nullptr;
}
};
Foo Get()
{
Foo f;
cout << &f << "f" <<endl;
return f;
}
void Set(Foo rhs)
{
Foo obj(rhs);
cout << &obj << "obj"<<endl;
}
int main()
{
Set(Get());
return 0;
}
output...
0x7fffe38fa0a0 Foo Constructor
0x7fffe38fa0a0 f
0x7fffe38fa070 Copy Constructor
0x7fffe38fa070 obj
0x7fffe38fa070 ~Foo Destructor
0x7fffe38fa0a0 ~Foo Destructor
Answer: Due to the Named Return Value Optimization the parameter rhs is constructed inplace as an alias of local variable f. (That is to say rhs and f are the same instance).
As rhs is an lvalue, the copy constructor is used to copy construct obj from rhs.