malloc.c:2372:sysmalloc:断言

时间:2016-10-28 18:55:57

标签: c++ malloc new-operator

/* Dynamic Programming implementation of LCS problem */
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<set>
using namespace std;


/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
int** lcs( char *X, char *Y, int m,int n)
{

int **L;
L = new int*[m];


/* Following steps build L[m+1][n+1] in bottom up fashion. Note
    that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
for (int i=0; i<=m; i++)
{
    L[i] = new int[n];

    for (int j=0; j<=n; j++)
    {
    if (i == 0 || j == 0)
        L[i][j] = 0;
    else if (X[i-1] == Y[j-1])
        L[i][j] = L[i-1][j-1] + 1;
    else
        L[i][j] = max(L[i-1][j], L[i][j-1]);
    }
}

return L;
}
void printlcs(char *X, char *Y,int m,int n,int *L[],string str)
{

    if(n==0 || m==0)
    {   cout<<str<<endl;
        return ;
    }
    if(X[m-1]==Y[n-1])
    {   str= str + X[m-1];
        //cout<<X[m-1];
        m--;
        n--;

        printlcs(X,Y,m,n,L,str);

    }else if(L[m-1][n]==L[m][n-1]){
        string str1=str;
        printlcs(X,Y,m-1,n,L,str);
        printlcs(X,Y,m,n-1,L,str1);
    }
    else if(L[m-1][n]<L[m][n-1])
    {
        n--;
        printlcs(X,Y,m,n,L,str);
    }
     else
    {
        m--;
        printlcs(X,Y,m,n,L,str);
    }

}
/* Driver program to test above function */
int main()
{
char X[] = "afbecd";
char Y[] = "fabced";
int m = strlen(X);
int n = strlen(Y);

int **L;
L=lcs(X, Y,m,n);
string str="";
printlcs(X,Y,m,n,(int **)L,str);
return 0;
}

这是用于打印所有可能最长的公共子序列的程序。如果我们提供输入char X[] = "afbecd";char Y[] = "fabced";,那么它显示以下错误,而对于输入char X[] = "afbec";char Y[] = "fabce",它正常工作。

solution: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.

可以请任何人弄清楚为什么会发生这种奇怪的行为。感谢

1 个答案:

答案 0 :(得分:1)

lcs函数中,在L循环中for数组的迭代过程中,您没有数组边界。 L是长度为m的数组:

int **L;
L = new int*[m];

在这个循环中:

for (int i=0; i<=m; i++)
{
  L[i] = new int[n];

您在L[m]时访问i == m元素。它是未定义的行为,因为从0索引的数组,这是对m + 1元素的访问。

在访问n + 1长度为L[i]的{​​{1}}数组中的n元素时,下一个循环中存在同样的问题:

for (int j=0; j<=n; j++)
{
  // Code skipped
  L[i][j] = 0;