Saturday 15 August 2015

c++ - Why does one of the two programs cause "*** glibc detected *** double free or corruption" error while the other not? -


According to Valgrind, this problem is due to the code given below. I want to remove the element in the list, which has the same integer value as the referee
  while (itr1! = List1.end ()) {ifr1-> num == ref ) {List1.erase (itr1); } Else {itr1 ++;}}   

list1 is an STL list, the type of list element is NODE, which is one of the composition integer element in NODE number one of the Iitrot 1 list There is a binary i ref is an integer value.


But I replace it with the code given below, it's true

 for  (; itr1! = List1.end (); itr1 ++ ) {If (itr1-> gt; num == ref) {list1.erase (itr1); Itr1--; }}   

I could not really see the difference between the two snippets.

I do not know that you can find out the problem with an incomplete code. If you need, I can post all the programs. Thanks!

deleted element is invalid after the erase second code destiny Works because it is undefined behavior and the code should be considered a buggy.

No comments:

Post a Comment