recursion - Recursively Delete Even Items from Doubly Linked List C++ -
here same question asking: deleting nodes doubly link list c++
the difference is, want understand wrong code. don't want have accept different approach without understand why code doesn't work. here 2 versions of code know wrong both. both of them give me segmentation fault.
int removeeven(node *&head) { if(!head) //base case: end of list reached return 0; int count = removeeven(head->next); //recurse end of list if(head->data % 2 != 0) return count; else{ ++count; if(head->next){ head->next->previous = head->previous; } if(head->previous){ head->previous->next = head->next; } if(!(head->previous)){ node* temp = head; head = head->next; delete temp; } else delete head; } return count; }
this second 1 takes count = 0 default arg.
int removeeven(node *&head, int count) if(head && head->data % 2 != 0) //not null, not { removeeven(head->next, count); } else if(head != null){ //not null, yes ++count; if(head->next) head->next->previous = head->previous; if(head->previous) head->previous->next = head->next; node* temp = head; head = head->next; delete temp; removeeven(head, count); } return count; //base case: null }
int removeeven(node *&head) { if(!head) //base case: end of list reached return 0; int count = removeeven(head->next); //recurse end of list if(head->data % 2 != 0) return count; else{ ++count; // correct way!!! copy old pointer in temp node *t = head; if(head->next){ head->next->previous = head->previous; } if(head->previous){ // warning!!! here modifying head nullptr head->previous->next = head->next; } // correct way!!! delete temp pointer delete t; // warning!!! here trying access nullptr in head // if(!(head->previous)){ // node* temp = head; // head = head->next; // delete temp; // } // else // delete head; } return count; }
i have got hint of root cause valgrind , gdb
valgrind ./a.out ==2729== memcheck, memory error detector ==2729== copyright (c) 2002-2013, , gnu gpl'd, julian seward et al. ==2729== using valgrind-3.10.0 , libvex; rerun -h copyright info ==2729== command: ./a.out ==2729== [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ==2729== invalid read of size 8 ==2729== @ 0x4008f5: removeeven(node*&) (list1.cpp:26) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x400afd: main (list1.cpp:81) ==2729== address 0x10 not stack'd, malloc'd or (recently) free'd ==2729== ==2729== ==2729== process terminating default action of signal 11 (sigsegv) ==2729== access not within mapped region @ address 0x10 ==2729== @ 0x4008f5: removeeven(node*&) (list1.cpp:26) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x40087a: removeeven(node*&) (list1.cpp:15) ==2729== 0x400afd: main (list1.cpp:81) ==2729== if believe happened result of stack ==2729== overflow in program's main thread (unlikely ==2729== possible), can try increase size of ==2729== main thread stack using --main-stacksize= flag. ==2729== main thread stack size used in run 8720384. ==2729== ==2729== heap summary: ==2729== in use @ exit: 240 bytes in 10 blocks ==2729== total heap usage: 10 allocs, 0 frees, 240 bytes allocated ==2729== ==2729== leak summary: ==2729== lost: 24 bytes in 1 blocks ==2729== indirectly lost: 0 bytes in 0 blocks ==2729== possibly lost: 0 bytes in 0 blocks ==2729== still reachable: 216 bytes in 9 blocks ==2729== suppressed: 0 bytes in 0 blocks ==2729== rerun --leak-check=full see details of leaked memory ==2729== ==2729== counts of detected , suppressed errors, rerun with: -v ==2729== error summary: 1 errors 1 contexts (suppressed: 0 0) compilation segmentation fault @ fri jul 28 09:46:51
Comments
Post a Comment