c++ - Segmentation fault with my Move assignment opertaor overloading -
note: bug in program in question has been find, , rearrange content of question. if have similar problem, post recommend check not move assignment operator destructor.
first, can see image know data structure of classes below. enter image description here
.h
class list{ public: // destructor ~list(); // default constructor list(); // move assignment operator list& operator=(list&& src); private: node* lead; ///boundary sentinel: dummy head node* new_dummy_head();
};
.cc
// destroctors list::~list(){ node* cur = lead->next; node* nex = cur->next; while(cur->data!=null){ delete cur; cur = nex; nex = nex->next; } delete lead; } // default constructors (just create dummy node) list::list(){ lead = new_dummy_head(); } // move assignment operator overloading list& list::operator=(list&& src){ // release resources *this owns , reset *this // not point of question, hide // pilfer src lead = src.lead; // reset source object src.lead = null; return *this; } node* list::new_dummy_head(){ node* ptr = new node; cerr << ptr->data << endl; ptr->prev = ptr->next = ptr; return ptr; }
main.cc
int main() { list beta; beta = list(); return 0; }
and get
segmentation fault
in program, destructor of list assume there's dummy node pointed lead
, can see in definition of move assignment operator
// reset source object src.lead = null; return *this;
it set lead
of rvalue object null, segmentation fault when destroy move-from object
Comments
Post a Comment