C++ 错误:malloc():排序的比较函数内存损坏

C++ 错误:malloc():排序的比较函数内存损坏,c++,stl,C++,Stl,以下代码用于打印整数列表中的最大数字。我得到: *** Error in `./a.out': malloc(): memory corruption: 0x0000000000bfe070 *** 在列表中(20个零): 然而,在上面,如果我放置一些非零元素,我不会得到错误 这是我的比较函数代码: bool comp(int a,int b) { if(a == b) return true; stringstream ss; ss <

以下代码用于打印整数列表中的最大数字。我得到:

 *** Error in `./a.out': malloc(): memory corruption: 0x0000000000bfe070 ***
在列表中(20个零):

然而,在上面,如果我放置一些非零元素,我不会得到错误

这是我的比较函数代码:

bool comp(int a,int b)
{
     if(a == b)
         return true;
     stringstream ss;
     ss << a;
     string a1 = ss.str();
     stringstream sss;
     sss << b;
     string b1 = sss.str();
     int i = 0;
     int l1 = a1.length();
     int l2 = b1.length();
     while(i < l1 && i < l2)
     {
         if(a1[i] > b1[i])
             return true;
         if(a1[i] < b1[i])
             return false;
         i++;
     }
     if(l1 == l2)
         return true;
     if(l1 < l2)
         if(b1[l1] > a1[0])
             return false;
         else
             return true;
     else
         if(a1[l2] > b1[0])
             return true;
         else
             return false;
}
其中nums是整数向量

编辑1:

这是全部代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>

using namespace std;
bool comp(int a,int b)
{
if(a == b)
    return true;
stringstream ss;
ss << a;
string a1 = ss.str();
stringstream sss;
sss << b;
string b1 = sss.str();
int i = 0;
int l1 = a1.length();
int l2 = b1.length();
while(i < l1 && i < l2)
{
    if(a1[i] > b1[i])
        return true;
    if(a1[i] < b1[i])
        return false;
    i++;
}
if(l1 == l2)
    return true;
if(l1 < l2)
    if(b1[l1] > a1[0])
        return false;
    else
        return true;
else
    if(a1[l2] > b1[0])
        return true;
    else
        return false;
}
void largestNumber(vector<int>& nums)
{

sort(nums.begin(),nums.end(),comp);
/*string s = "";
vector<int>::iterator it = nums.begin();
while(it != nums.end())
{
    stringstream ss;
    ss << *it;
    s = s+ss.str();
    it++;
}
return s;*/
}



int main()
{
int n;
cin>>n;
vector<int> arr(n);
for(int i = 0;i<n;i++)
    cin>>arr[i];

largestNumber(arr);/*
string s = largestNumber(arr);
cout<<s<<endl;*/
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
布尔公司(内部a、内部b)
{
如果(a==b)
返回true;
细流ss;
ss a1[0])
返回false;
其他的
返回true;
其他的
如果(a1[l2]>b1[0])
返回true;
其他的
返回false;
}
最大无效数(向量和nums)
{
排序(nums.begin(),nums.end(),comp);
/*字符串s=“”;
向量::迭代器it=nums.begin();
while(it!=nums.end())
{
细流ss;
ss>n;
向量arr(n);
对于(int i=0;i>arr[i];
最大数量(arr)/*
字符串s=最大数字(arr);

cout我已经使用了您的
comp
函数,并创建了一个示例案例研究来检查错误。但是,我没有发现关于
零数组的任何错误。请检查示例程序

错误 但是,下面的场景会导致运行时错误

// Pay attention: Array stores numbers in descending order.
// If we stored in ascending order, there won't be a problem.
for(int i=20; i>0; ++i)
    v.push_back(i);
我希望这将有助于找到解决办法

示例程序
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
布尔公司(内部a、内部b)
{
如果(a==b)
返回true;
细流ss;
ss b1[l1-1])
返回false;
其他的
返回true;
其他的
如果(a1[l2]>a1[l2-1])
返回true;
其他的
返回false;
}
int main(int argc,const char*argv[]
{
向量v;

对于(int i=0;i您的comp函数违反了。Sort要求比较函数应满足严格的弱排序规则。如果该承诺被打破,则std::Sort的承诺也会被破坏以正确运行。事实上,当我在MSVC(VS2015)下编译此函数时,我得到了一个断言失败,即比较函数不满足排序条件。 对于这一行:

 if(a == b)
     return true;
显然违反了条件。查看帖子了解更多信息

顺便说一句,如果你只想按字典顺序对整数排序,你可以这样做

bool comp(int a,int b)
{
    return to_string(a) < to_string(b);
}
bool comp(内部a、内部b)
{
返回到字符串(a)<返回到字符串(b);
}

如果你想要“先大一点的数字”,只需交换

为什么要将整数转换为字符串并比较字符串,而不是简单地直接比较整数?请尝试使用valgrind运行您的程序。这对于查找程序无意中从不应该写入/读取的区域写入/读取的错误非常有用。您得到的错误意味着程序正如写入堆中未分配给用户的部分一样。这很奇怪……我已经注释掉了所有其他内容,但仍然存在错误。@midi我发现了一个错误,并用示例程序指出,请检查它。我刚刚检查了您的代码..我的机器上出现了分段错误。ubuntu64bit@midi也许是因为你的案子,库请分享完整的代码。顺便说一句,我在你的实现中发现了一个错误,你怎么看?l1是字符串a1的长度,l2是字符串b1的长度。如果l1是1,l2是2,那么b1的长度是2,因此b1[0]和b1[1]根据记录,它不会发生在Microsoft Visual Studio 2012版本11.0.50727.1中。@MuratKarakuş是否在调试模式下编译?@MuratKarakuş:“在我的机器上工作”并不意味着它是正确的。在平等性上返回
true
显然违反了发布模式下的断言。@MuratKarakuş@MuratKarakuş:我实际上看到一个无效的比较函数导致
std::sort
修改超过
std::vector
末尾的内存。违反函数的先决条件很容易导致未定义的行为。
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <iterator>

using namespace std;

bool comp(int a,int b)
{
     if(a == b)
         return true;
     stringstream ss;
     ss << a;
     string a1 = ss.str();
     stringstream sss;
     sss << b;
     string b1 = sss.str();
     int i = 0;
     int l1 = a1.length();
     int l2 = b1.length();
     while(i < l1 && i < l2)
     {
         if(a1[i] > b1[i])
             return true;
         if(a1[i] < b1[i])
             return false;
         i++;
     }
     if(l1 == l2)
         return true;
     if(l1 < l2)
         if(b1[l1] > b1[l1-1])
             return false;
         else
             return true;
     else
         if(a1[l2] > a1[l2-1])
             return true;
         else
             return false;
}

int main(int argc, const char *argv[])
{
    vector<int> v;
    for(int i=0; i<20; ++i)
        v.push_back(0);
    sort(v.begin(), v.end(), comp);
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    return 0;
}
 if(a == b)
     return true;
bool comp(int a,int b)
{
    return to_string(a) < to_string(b);
}