std::sort引起的core

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/eric509/article/details/84917861
  1. /// This is a helper function...
  2.   template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
  3.     _RandomAccessIterator
  4.     __unguarded_partition(_RandomAccessIterator __first,
  5.              _RandomAccessIterator __last,
  6.              const _Tp& __pivot, _Compare __comp)
  7.     {
  8.       while (true)
  9.     {
  10.      while (__comp(*__first, __pivot))
  11.      ++__first;
  12.      --__last;
  13.      while (__comp(__pivot, *__last))
  14.      --__last;
  15.      if (!(__first < __last))
  16.      return __first;
  17.      std::iter_swap(__first, __last);
  18.      ++__first;
  19.     }
  20.     }

此函数完成快速排序中分区功能,即将比哨兵小的数据放在其前,大的放在其后。

函数中使用的是
while (__comp(*__first, __pivot))
    ++__first;

如果当比较元素相同返回真时,此时比较元素将会继续向下遍历,在极端情况下,例如程序中所有元素都是一样的情况下,在这种情况下,就会出现访问越界,结果就是导致程序出现segment fault

所以在写c++ stl中的比较函数是,bool返回真的时候,一定是“真的”大,或者小,等于的时候只能返回false。

展开阅读全文

使用std::sort遇到难题

01-27

好的,首先,这是我的成员函数,这个成员函数可以取代oprater>重载,会读取到许多private变量rnbool smsOP::time_compare(const sms_elem & p1,const sms_elem & p2)rnrn date d1,d2;rnrn d1 = processTime(p1);rn d2 = processTime(p2);rnrn if (d1.year > d2.year)rn return true;rn else rn if (d1.month > d2.month)rn return true;rn elsern if (d1.day > d2.day)rn return true;rn elsern if (d1.hour > d2.hour)rn return true;rn elsern if (d1.min > d2.min)rn return true;rn elsern if (d1.sec > d2.sec)rn return true;rn elsern return false;rnrn好的,这个函数通过测试。我在另外一个函数中调用,比如,这样调用rnsort(read_in.begin(),read_in.end(),time_compare);rn好,立马编译报错:rnj:\md\visual studio 2005\projects\smswork\smswork\smsop.cpp(122) : error C3867: 'smsOP::time_compare': function call missing argument list; use '&smsOP::time_compare' to create a pointer to memberrnj:\md\visual studio 2005\projects\smswork\smswork\smsop.cpp(122) : error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 providedrn d:\program files\microsoft visual studio 8\vc\include\algorithm(3109) : see declaration of 'std::sort'rnrn好的,改,改成这个这个样子:rnsort(read_in.begin(),read_in.end(),&smsOP::time_compare);rnrn继续报错:rnd:\program files\microsoft visual studio 8\vc\include\algorithm(3181) : error C2064: term does not evaluate to a function taking 2 argumentsrn d:\program files\microsoft visual studio 8\vc\include\algorithm(3237) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Unguarded_partition<_RanIt,_Pr>(_RanIt,_RanIt,_Pr)' being compiledrn withrn [rn _Ty1=std::_Vector_iterator>,rn _Ty2=std::_Vector_iterator>,rn _RanIt=std::_Vector_iterator>,rn _Pr=bool (__thiscall smsOP::* )(const sms_elem &,const sms_elem &)rn ]rn d:\program files\microsoft visual studio 8\vc\include\algorithm(3261) : see reference to function template instantiation 'void std::_Sort,__w64 int,_Pr>(_RanIt,_RanIt,_Diff,_Pr)' being compiledrn withrn [rn _Ty=sms_elem,rn _Alloc=std::allocator,rn _Pr=bool (__thiscall smsOP::* )(const sms_elem &,const sms_elem &),rn _RanIt=std::_Vector_iterator>,rn _Diff=__w64 intrn ]rn j:\md\visual studio 2005\projects\smswork\smswork\smsop.cpp(122) : see reference to function template instantiation 'void std::sort,bool(__thiscall smsOP::* )(const sms_elem &,const sms_elem &)>(_RanIt,_RanIt,_Pr)' being compiledrn withrn [rn _Ty=sms_elem,rn _Alloc=std::allocator,rn _RanIt=std::_Vector_iterator>,rn _Pr=bool (__thiscall smsOP::* )(const sms_elem &,const sms_elem &)rn ]rnd:\program files\microsoft visual studio 8\vc\include\algorithm(3181) : fatal error C1903: unable to recover from previous error(s); stopping compilationrnrn好了,太多了,不知道怎么解决,希望大家帮帮忙。 论坛

std::vector 析构的时候core

12-08

*** glibc detected *** SettRate: free(): invalid pointer: 0x0000000000aaafd0 ***rn======= Backtrace: =========rn/lib64/libc.so.6(+0x75018)[0x7f8b9df2b018]rn/lib64/libc.so.6(cfree+0x6c)[0x7f8b9df2ff6c]rn/home/sett13/QuickMDB/lib/libmdbInterface.so(_ZN12TMdbRollbackD1Ev+0x1c1)[0x7f8ba0f734d7]rn/home/sett13/QuickMDB/lib/libmdbInterface.so(_ZN12TMdbDatabase10DisconnectEv+0x83)[0x7f8ba0f59301]rn/home/sett13/QuickMDB/lib/libmdbInterface.so(_ZN12TMdbDatabaseD1Ev+0x16)[0x7f8ba0f59444]rn/home/sett13/settv80/lib/libsettappcomm.so(_ZN13TAppComponent7cleanupEv+0x1a)[0x7f8ba0823d2a]rn/home/sett13/settv80/lib/libsettappcomm.so(_ZN13TAppComponentD2Ev+0x24)[0x7f8ba08244d4]rn/home/sett13/settv80/lib/libsettappcomm.so(_ZN16TBaseApplicationD2Ev+0x321)[0x7f8ba0827571]rnSettRate[0x41ce17]rnSettRate[0x423a8c]rn/lib64/libc.so.6(__libc_start_main+0xe6)[0x7f8b9ded4bc6]rnSettRate[0x410ca9]rn======= Memory map: ========rn00400000-0053a000 r-xp 00000000 fd:01 164982 /home/sett13/settv80/bin/SettRatern0073a000-0073c000 r--p 0013a000 fd:01 164982 /home/sett13/settv80/bin/SettRatern0073c000-00764000 rw-p 0013c000 fd:01 164982 /home/sett13/settv80/bin/SettRatern00764000-00bac000 rw-p 00000000 00:00 0 [heap]rn7f8af8000000-7f8af8021000 rw-p 00000000 00:00 0 rn7f8af8021000-7f8afc000000 ---p 00000000 00:00 0 rn7f8afccc4000-7f8afd51c000 rw-p 00000000 00:00 0 rn7f8afd51c000-7f8b1d51c000 rw-s 00000000 00:04 4325380 /SYSV0376831e (deleted)rn7f8b1d51c000-7f8b3d51c000 rw-s 00000000 00:04 4292611 /SYSV03765c0e (deleted)rn7f8b3d51c000-7f8b4d51c000 rw-s 00000000 00:04 4358149 /SYSV0376545d (deleted)rn7f8b4d51c000-7f8b6d51c000 rw-s 00000000 00:04 4259842 /SYSV03765c2b (deleted)rn7f8b6d51c000-7f8b8d51c000 rw-s 00000000 00:04 4227073 /SYSV03765c2a (deleted)rn7f8b8d51c000-7f8b9d51c000 rw-s 00000000 00:04 4194304 /SYSV03765454 (deleted)rn7f8b9d51c000-7f8b9d5e4000 r-xp 00000000 fd:04 176869 /usr/lib64/libstdc++.so.5.0.7rn7f8b9d5e4000-7f8b9d7e3000 ---p 000c8000 fd:04 176869 /usr/lib64/libstdc++.so.5.0.7rn7f8b9d7e3000-7f8b9d7e5000 r--p 000c7000 fd:04 176869 /usr/lib64/libstdc++.so.5.0.7rn7f8b9d7e5000-7f8b9d7ec000 rw-p 000c9000 fd:04 176869 /usr/lib64/libstdc++.so.5.0.7rn7f8b9d7ec000-7f8b9d7fe000 rw-p 00000000 00:00 0 rn7f8b9d7fe000-7f8b9d813000 r-xp 00000000 fd:00 57509 /lib64/libnsl-2.11.1.sorn7f8b9d813000-7f8b9da12000 ---p 00015000 fd:00 57509 /lib64/libnsl-2.11.1.sorn7f8b9da12000-7f8b9da13000 r--p 00014000 fd:00 57509 /lib64/libnsl-2.11.1.sorn7f8b9da13000-7f8b9da14000 rw-p 00015000 fd:00 57509 /lib64/libnsl-2.11.1.sorn7f8b9da14000-7f8b9da16000 rw-p 00000000 00:00 0 rn7f8b9da16000-7f8b9dd05000 r-xp 00000000 fd:06 436351 /oracle/product/102/lib/libnnz10.sorn7f8b9dd05000-7f8b9de04000 ---p 002ef000 fd:06 436351 /oracle/product/102/lib/libnnz10.sorn7f8b9de04000-7f8b9deb5000 rw-p 002ee000 fd:06 436351 /oracle/product/102/lib/libnnz10.sorn7f8b9deb5000-7f8b9deb6000 rw-p 00000000 00:00 0 rn7f8b9deb6000-7f8b9e00a000 r-xp 00000000 fd:00 57555 /lib64/libc-2.11.1.sorn7f8b9e00a000-7f8b9e20a000 ---p 00154000 fd:00 57555 /lib64/libc-2.11.1.sorn7f8b9e20a000-7f8b9e20e000 r--p 00154000 fd:00 57555 /lib64/libc-2.11.1.sorn7f8b9e20e000-7f8b9e20f000 rw-p 00158000 fd:00 57555 /lib64/libc-2.11.1.sorn7f8b9e20f000-7f8b9e214000 rw-p 00000000 00:00 0 rn7f8b9e214000-7f8b9e22a000 r-xp 00000000 fd:00 57361 /lib64/libgcc_s.so.1rn7f8b9e22a000-7f8b9e429000 ---p 00016000 fd:00 57361 /lib64/libgcc_s.so.1rn7f8b9e429000-7f8b9e42a000 r--p 00015000 fd:00 57361 /lib64/libgcc_s.so.1rn7f8b9e42a000-7f8b9e42b000 rw-p 00016000 fd:00 57361 /lib64/libgcc_s.so.1rn7f8b9e42b000-7f8b9e480000 r-xp 00000000 fd:00 57489 /lib64/libm-2.11.1.sorn7f8b9e480000-7f8b9e67f000 ---p 00055000 fd:00 57489 /lib64/libm-2.11.1.sorn7f8b9e67f000-7f8b9e680000 r--p 00054000 fd:00 57489 /lib64/libm-2.11.1.sorn7f8b9e680000-7f8b9e681000 rw-p 00055000 fd:00 57489 /lib64/libm-2.11.1.sorn7f8b9e681000-7f8b9e771000 r-xp 00000000 fd:04 176641 /usr/lib64/libstdc++.so.6.0.10rn7f8b9e771000-7f8b9e970000 ---p 000f0000 fd:04 176641 /usr/lib64/libstdc++.so.6.0.10rn7f8b9e970000-7f8b9e977000 r--p 000ef000 fd:04 176641 /usr/lib64/libstdc++.so.6.0.10rn7f8b9e977000-7f8b9e979000 rw-p 000f6000 fd:04 176641 /usr/lib64/libstdc++.so.6.0.10rn7f8b9e979000-7f8b9e98c000 rw-p 00000000 00:00 0 rn7f8b9e98c000-7f8b9e990000 r-xp 00000000 fd:01 680047 /home/sett13/settv80/lib/libsettpreplus_selfdef.sorn7f8b9e990000-7f8b9eb8f000 ---p 00004000 fd:01 680047 /home/sett13/settv80/lib/libsettpreplus_selfdef.sorn7f8b9eb8f000-7f8b9eb90000 r--p 00003000 fd:01 680047 /home/sett13/settv80/lib/libsettpreplus_selfdef.sorn7f8b9eb90000-7f8b9eb91000 rw-p 00004000 fd:01 680047 /home/sett13/settv80/lib/libsettpreplus_selfdef.sorn7f8b9eb91000-7f8b9ef61000 rw-p 00000000 00:00 0 rn7f8b9ef61000-7f8b9ef63000 r-xp 00000000 fd:01 680049 /home/sett13/settv80/lib/libsettproc_cdr_module.sorn7f8b9ef63000-7f8b9f163000 ---p 00002000 fd:01 680049 /home/sett13/settv80/lib/libsettproc_cdr_module.sorn7f8b9f163000-7f8b9f164000 r--p 00002000 fd:01 680049 /home/sett13/settv80/lib/libsettproc_cdr_module.sorn7f8b9f164000-7f8b9f165000 rw-p 00003000 fd:01 680049 /home/sett13/settv80/lib/libsettproc_cdr_module.soAbortedrnrn最上面的地址0x0000000000aaafd0 是std::vector 里面出错的free的地址。rnrnrn问题:rn标准库的vector是否存在内存管理的漏洞,还是代码存在某种bug,导致内存被不明数据踩到了 论坛

使用std::list和sort方法编译时提示如下错误。

03-27

#include "stdafx.h"rnrn// alg_sort.cpprn// compile with: /EHscrn#include rn#include rn#include // For greater( )rn#include rn#include rnrn// Return whether first element is greater than the secondrnrnrnclass CBaseArnrnpublic:rn CBaseA(int nIndex)rn :m_Int(nIndex)rn rn rn int m_Int;rnprotected:rnprivate:rn;rnrnbool UDgreater ( CBaseA *elem1, CBaseA *elem2 )rnrn return true;rnrnrnint main( )rnrn using namespace std;rnrn //可以编译通过。rn //vector v1;rn //vector::iterator Iter1;rnrn //编译不过提示:rn list v1;rn list ::iterator Iter1;rnrn int i;rn for ( i = 0 ; i <= 5 ; i++ )rn rn v1.push_back(new CBaseA( 2 * i ));rn rnrn int ii;rn for ( ii = 0 ; ii <= 5 ; ii++ )rn rn v1.push_back( new CBaseA( 2 * ii + 1 ));rn rnrn sort( v1.begin( ), v1.end( ), UDgreater );rnrnrn/*rn//使用 std::list提示以下错误。怎么解决?rn>e:\microsoft visual studio 8\vc\include\algorithm(3261) : error C2784: 'reverse_iterator<_RanIt>::difference_type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'rnwithrn[rn _Ty=CBaseA *,rn _Secure_validation=truern]rne:\microsoft visual studio 8\vc\include\xutility(1856) : see declaration of 'std::operator -'rne:\documents and settings\administrator\desktop\sdfwert\sdfwert\sdfwert.cpp(58) : see reference to function template instantiation 'void std::sort::_Iterator<_Secure_validation>,bool(__cdecl *)(CBaseA *,CBaseA *)>(_RanIt,_RanIt,_Pr)' being compiledrn withrn [rn _Ty=CBaseA *,rn _Secure_validation=true,rn _RanIt=std::list::_Iterator,rn _Pr=bool (__cdecl *)(CBaseA *,CBaseA *)rn ]rn*/ 论坛

[求助]是什么原因引起这个Core,如何分析?

09-20

硬件:两台IBM P650 CPU×8 内存 8GB rn操作系统:AIX 5L 5200-02 rnoracle版本:ORACLE 9i (9.2.0.4) RAC 使用fast存贮rnrn问题:运行errpt -a 打印错误报告,错误报告如下所示。rnrnlt3:/#errpt -aj B6048838|pgrn---------------------------------------------------------------------------rnLABEL: CORE_DUMPrnIDENTIFIER: B6048838rnrnDate/Time: Thu Sep 15 21:00:11 TAISrnSequence Number: 31087rnMachine Id: 0052853A4C00rnNode Id: zzlt3rnClass: SrnType: PERMrnResource Name: SYSPROC rnrnDescriptionrnSOFTWARE PROGRAM ABNORMALLY TERMINATEDrnrnProbable CausesrnSOFTWARE PROGRAMrnrnUser CausesrnUSER GENERATED SIGNALrnrnRecommended ActionsrnCORRECT THEN RETRYrnrnFailure CausesrnSOFTWARE PROGRAMrnrnRecommended ActionsrnRERUN THE APPLICATION PROGRAMrnIF PROBLEM PERSISTS THEN DO THE FOLLOWINGrnCONTACT APPROPRIATE SERVICE REPRESENTATIVErnrnDetail DatarnSIGNAL NUMBERrn6rnUSER'S PROCESS ID:rn676082rnFILE SYSTEM SERIAL NUMBERrn10rnINODE NUMBERrn226601rnPROCESSOR IDrn3rnCORE FILE NAMErn/oracle/app/oracle/admin/zzlt/cdump/core_676082/corernCORRECT THEN RETRYrnrnFailure CausesrnSOFTWARE PROGRAMrnrnRecommended ActionsrnRERUN THE APPLICATION PROGRAMrnIF PROBLEM PERSISTS THEN DO THE FOLLOWINGrnCONTACT APPROPRIATE SERVICE REPRESENTATIVErnrnDetail DatarnSIGNAL NUMBERrn6rnUSER'S PROCESS ID:rn676082rnFILE SYSTEM SERIAL NUMBERrn10rnINODE NUMBERrn226601rnPROCESSOR IDrn3rnCORE FILE NAMErn/oracle/app/oracle/admin/zzlt/cdump/core_676082/corernPROGRAM NAMErnoraclernADDITIONAL INFORMATIONrnslcra 3F8rn??rnssexhd 2C0rn??rn??rnrnSymptom DatarnREPORTABLErn1rnINTERNAL ERRORrn0rnSYMPTOM CODErnPCSS/SPI2 FLDS/oracle SIG/6 FLDS/slcra VALU/3f8rn---------------------------------------------------------------------------rnrnlt4:/#errpt -aj B6048838|pgrn---------------------------------------------------------------------------rnLABEL: CORE_DUMPrnIDENTIFIER: B6048838rnrnDate/Time: Fri Sep 16 11:51:03 2005rnSequence Number: 7382rnMachine Id: 0052852A4C00rnNode Id: zzlt4rnClass: SrnType: PERMrnResource Name: SYSPROCrnrnDescriptionrnSOFTWARE PROGRAM ABNORMALLY TERMINATEDrnrnProbable CausesrnSOFTWARE PROGRAMrnrnUser CausesrnUSER GENERATED SIGNALrnrnRecommended ActionsrnCORRECT THEN RETRYrnrnFailure CausesrnSOFTWARE PROGRAMrnrnRecommended ActionsrnRERUN THE APPLICATION PROGRAMrnIF PROBLEM PERSISTS THEN DO THE FOLLOWINGrnCONTACT APPROPRIATE SERVICE REPRESENTATIVErnrnDetail DatarnSIGNAL NUMBERrn11rnUSER'S PROCESS ID:rn958638rnFILE SYSTEM SERIAL NUMBERrn1rnINODE NUMBERrn2rnPROCESSOR IDrn6rnCORE FILE NAMErn/corernPROGRAM NAMErndbspicao90rnADDITIONAL INFORMATIONrn??rn??rn??rn??rn??rn??rn??rn__start 8CrnUnable to generate symptom string.rn---------------------------------------------------------------------------rnrn请问:如何能找到导致coredump的程序和原因,如何解决这个问题!请指教,不甚感激!!! 论坛

没有更多推荐了,返回首页