c++ - copy constructor undefined behavior -


below code snippet used learning. here trying create linked-list class. i'm yet define proper copy ctor ; but, expecting compiler supplied copy ctor atleast copy value of head pointer didn't happen:-

#include<iostream>  using namespace std;  class list; class listnode { int idata; char cdata; listnode *next;  public: listnode(); listnode(int , char , listnode *); listnode* operator++(int); friend class list; };  listnode::listnode() :idata(int()), cdata(char()), next(null) {     cout<<"default ctor"<<endl; }  listnode::listnode(int i, char c, listnode *argnext) :idata(i), cdata(c), next(argnext) { cout<<"param ctor idata: "<<this->idata<<" cdata: "<<this->cdata<<" next: "<<this->next<<endl; }  listnode* listnode::operator++(int) {     cout<<"postfix operator overload"<<endl;     return this->next; }  class list {     listnode *head;      public:     list();     /*     list(const list &reflist) {         cout<<"copy ctor list"<<endl;         this->head = reflist.head;     }     */     void operator=(const list &) {cout<<"assignment operator list"<<endl; }     int insert(listnode *newnode);     void printlist() const; }; list::list() :head(null) {     cout<<"creating empty list"<<endl; }  int list::insert(listnode *newnode) {   ... <code not included>  }  void list::printlist() const {     listnode *itr = head;      cout<<"printing list: "<<head<<endl;     ( ; itr; itr = (*itr)++) {         cout<<&itr->idata<<" : "<<itr->idata<<" ";     }     cout<<endl; }  int main() {     list newlist;     list copylist = newlist;     newlist.printlist();     copylist.printlist();    ...     return 0; } 

now when use below code make use of list class :-

int main() {     list newlist;     list copylist = newlist; <<< expecting copy ctor make head of copylist null     newlist.printlist();     copylist.printlist(); ... 

however seeing head pointer of copylist created compiler supplied copy ctor having value of 0x2 instead of null(0x0). why ??? able check address of head pointer of copylist using below function:-

void list::printlist() const { listnode *itr = head;  cout<<"printing list: "<<head<<endl; ( ; itr; itr = (*itr)++) {     cout<<&itr->idata<<" : "<<itr->idata<<" "; } cout<<endl; } 

because of unexpected behavior of compiler's copy ctor results in segmentation fault. avoided defining own copy ctor(not functional):-

/* list(const list &reflist) {     cout<<"copy ctor list"<<endl;     this->head = reflist.head; } */ 

one interesting observation after defined above copy ctor compiled , verified seg fault has disappeared since copylist head null. after verification again commented out above copy ctor , compiled again. time didn't face issue of compiler's copy ctor setting head of copylist 0x2.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -