malloc.c:2372: sysmalloc: Assertion

malloc.c:2372: sysmalloc: Assertion

本文关键字:Assertion sysmalloc 2372 malloc      更新时间:2023-10-16
/* 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.
谁能解释一下为什么会出现这种奇怪的行为?由于

lcs函数中,您在for循环中迭代L数组时超出了数组界限。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元素的访问。

同样的问题出现在下一个循环中访问长度为nL[i]数组中的n + 1元素时:

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