gpt4 book ai didi

c++ - malloc.c :2372: sysmalloc: Assertion

转载 作者:行者123 更新时间:2023-11-28 05:27:06 30 4
gpt4 key购买 nike

/* 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;
}

这是打印所有可能的最长公共(public)子序列的程序。如果我们输入 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];

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

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

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

关于c++ - malloc.c :2372: sysmalloc: Assertion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311550/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com