Std::nth_element Crash question _php tutorial

Source: Internet
Author: User

Std::nth_element crash problems


(1) Source code:

 
  
  
  1. Auto Less_compare = [] (const mirroringgroup& MG1, const mirroringgroup& MG2), bool {
  2. Return (Mg1.usage () < Mg2.usage ());
  3. };

(2) Questions:

Often occurs crash,stack as follows:

 
 
  1. #0 0x00000000004b3807 in Mirroringgroup::copyfrom (This=0x15edf20, from= ...) at miuifs/miuistorage-dev/idl/proto/ internaldata.pb.cc:6487
  2. #1 0x000000000052bc71 in mirroringgroup::operator= (This=0x15edf20, from= ...) at miuifs/miuistorage-dev/idl/proto/ internaldata.pb.h:1797
  3. #2 0X000000000052F7CB in Std::swap (__a= ..., __b= ...) at/usr/local/include/c++/4.8.2/bits/move.h:177
  4. #3 0x000000000052e0b0 in Std::iter_swap<__gnu_cxx::__normal_iterator >, __gnu_cxx:: __normal_iterator > > > (__a=, __b= ...)
  5. at/usr/local/include/c++/4.8.2/bits/stl_algobase.h:147
  6. #4 0x0000000000604b11 in Std::__unguarded_partition<__gnu_cxx::__normal_iterator , Mirroringgroup, miuifs::blockmanager::choosewritablemirroringgroups (std::vector) mirroringgroup> *, int)::__lambda101> (__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator >, const mirroringgroup &, miuifs::blockmanager::__lambda101) (__fi Rst= ..., __last= ..., __pivot= ..., __comp= ...) at/usr/local/include/c++/4.8.2/bits/stl_algo.h:2270
  7. #5 0x0000000000603c1b in Std::__unguarded_partition_pivot<__gnu_cxx::__normal_iterator ;, miuifs :: Blockmanager::choosewritablemirroringgroups (std::vector *, int)::__lambda101> (__gnu_cxx::__normal_ Iterator >, __gnu_cxx::__normal_iterator > > , miuifs::blockmanager::__lambda101) (
  8. __first= ..., __last= ..., __comp= ...) at/usr/local/include/c++/4.8.2/bits/stl_algo.h:2296
  9. #6 0x0000000000603408 in Std::__introselect<__gnu_cxx::__normal_iterator ;, long int, miuifs::blo Ckmanager::choosewritablemirroringgroups (std::vector *, int)::__lambda101> (__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator >, __gnu_cxx::_ _normal_iterator >, Long, miuifs::blockmanager::__lambda101) (__first= ..., _ _nth= ..., __last= ..., __depth_limit=2,
  10. __comp= ...) at/usr/local/include/c++/4.8.2/bits/stl_algo.h:2394
  11. #7 0x0000000000602c95 in Std::nth_element<__gnu_cxx::__normal_iterator ;, Miuifs::blockmanager:: Choosewritablemirroringgroups (std::vector *, int)::__lambda101> (__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator >;, __gnu_cxx::__normal_ite Rator >, miuifs::blockmanager::__lambda101) (__first= ..., __nth= ..., __last= ..., __comp= ...)
  12. at/usr/local/include/c++/4.8.2/bits/stl_algo.h:5417
  13. #8 0x000000000060039c in Miuifs::blockmanager::choosewritablemirroringgroups (THIS=0X118ABE0 , mgs=0x7fffeb9f414 0,
  14. copy_count=2) at miuifs/miuistorage-dev/blockmanager.cc:391
  15. #9 0x00000000005ff9cf in Miuifs::blockmanager::newblock (THIS=0X118ABE0 ) at Miuifs/miuistorage-dev/blockmanager . cc:331
  16. #10 0x00000000005fed63 in Miuifs::blockmanager::acquireblock (THIS=0X118ABE0 , attribute= ...)
  17. At miuifs/miuistorage-dev/blockmanager.cc:243

(3) Find the problem:

Problem has been in the std::nth_element, began to think of STL is not the problem, has not been a good solution, and then read the STL source code to find the reason in/usr/local/include/c++/4.8.2/bits/stl_ In Algo.h:

 
  
  
  1. Template
  2. Inline _randomaccessiterator
  3. __unguarded_partition_pivot (_randomaccessiterator __first,
  4. _randomaccessiterator __last, _compare __comp)
  5. {
  6. _randomaccessiterator __mid = __first + (__last-__first)/2;
  7. Std::__move_median_to_first (__first, __first + 1, __mid, (__last-2),
  8. __COMP);
  9. Return Std::__unguarded_partition (__first + 1, __last, *__first, __comp);

The role of the __move_median_to_first function is to Exchange __first +1, __mid, (__last-2) in the middle size of the value and __first. But ignoring the __mid, (__last-2) points to the same iterator, if the input is as follows:


After __move_median_to_first, the results are as follows:


At this point, __first points to the maximum value. Then look at the implementation of Std::__unguarded_partition, in 2263 rows __comp (*__first, __pivot)) forever returns True, causing ++__first to execute and access the illegal memory.


 
  
  
  1. Template
  2. _randomaccessiterator
  3. __unguarded_partition (_randomaccessiterator __first,
  4. _randomaccessiterator __last,
  5. Const _tp& __pivot, _compare __comp)
  6. {
  7. while (true)
  8. {
  9. while (__comp (*__first, __pivot))
  10. ++__first;
  11. --__last;
  12. while (__comp (__pivot, *__last))
  13. --__last;
  14. if (! ( __first < __last))
  15. return __first;
  16. Std::iter_swap (__first, __last);
  17. ++__first;
  18. }

(4) Workaround:

Through Google find the following link, found that is really a STL bug, can only be resolved by upgrading C + +.

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=732042





http://www.bkjia.com/PHPjc/1071768.html www.bkjia.com true http://www.bkjia.com/PHPjc/1071768.html techarticle std::nth_element crash problem (1) Source: Auto Less_compare = [] (const mirroringgroup MG2), bool {return (Mg1.usage ()}; Std::nth_element (Mgs->begin (), Mgs->begin () + (Copy_co ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.