c++ - Seg fault in copy constructor doubly linked list -


so, when try set data of newptr equal data of origptr, receive seg fault within first lopp of for-control-structure. i'm not sure causing it. maybe misuse of pointers? or typo? or perhaps range error in loop's idiom?

list::list(const list &alist): numnodes(alist.numnodes) {     empty = true;     forward = true;     string flag;      cout << "copy head tail? (y/n): ";     cin >> flag;     if(flag=="n")         forward = false;      if(!alist.head) {         head = null; //alist empty. this->list.         tail = null;     } else { // copy 1st node.         head = new node;         if(forward)             head->setdata(alist.head->getdata());         else // copy in reverse.             head->setdata(alist.tail->getdata());         //copy rest of list.         node *newptr = head; //newptr points last node in new list.          //origptr points nodes in original list.         if(forward) {             cout << "copying normally...\n" << endl;             for(node *origptr=alist.head->getnext(); origptr!=null;                 origptr=origptr->getnext()) {                 newptr = newptr->getnext();                 newptr->setdata(origptr->getdata()); //seg fault             } // end             cout << "3" << endl;         } else {             cout << "copying in reverse order...\n" << endl;             for(node *origptr=alist.tail->getprev(); origptr!=null;                 origptr=origptr->getprev()) {                 newptr = newptr->getnext();                 newptr->setdata(origptr->getdata()); //seg fault             } // end         } // end if/else         newptr->setnext(null);     } // end if/else     cout << "done copying!\n" << endl; } // end copy constructor 

if more code necessary, make necessary edits.

also, aware standard c++11 use nullptr. not using implementation. i'm running on ubuntu 12.04 gcc-v4.6.3, not support c++11.

edit: guys! added 3 lines loops before newptr points getnext(). declared newnode node pointer pointing null on next line after define newptr pointing head.

            newnode = new node;             newnode->setprev(newptr);             newptr->setnext(newnode); 

let analyze following code.

  head = new node;     if(forward)         head->setdata(alist.head->getdata());     else // copy in reverse.         head->setdata(alist.tail->getdata());     //copy rest of list.     node *newptr = head; //newptr points last node in new list. 

here, creating object of node , assigning head. setting last or first data of list in head pointer. assing newptr head. till newptr pointing 1 element of list.

next let analyze following code.

} else {     cout << "copying in reverse order...\n" << endl;     for(node *origptr=alist.tail->getprev(); origptr!=null;         origptr=origptr->getprev()) {         newptr = newptr->getnext();         newptr->setdata(origptr->getdata()); //seg fault     } // end 

here doing

newptr = newptr->getnext() 

now, since newptr pointing 1 element. after operation, newptr becomes null. now, newptr being null, following bound crash

newptr->setdata(origptr->getdata()); //seg fault 

you must allocate memory next element before doing newptr->getnext().


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -