包子卖完了啊 2016-06-25 21:04 采纳率: 100%
浏览 1744

c++ :memory clobbered past end of allocated block

代码如下计算kmp算法中的next[],在DEV-c++中测试时,当字符串str的长度为5时会运行不出来,长度为其他时能正常运行,在在线编译软件中测试时无论str的长度为多少都会出现:memory clobbered past end of allocated block
#include
#include
#include
using namespace std;
void strF(string str, int * f)
{
f [0] = 0;
f [1] = 0;
int k;
int length = str.length();
for(int i = 1; i < length + 1; i++)
{
k = 0;
for(int j = 1; j <= i; j++)
{
if(str.compare(0, j - 1, str, i - j + 1, j - 1) == 0)
{
k = j;
}
}
f [i + 1] = k;
}
}
int * strNext(string str)
{
int * next = new int [str.length() + 1];
int * f = new int [str.length() + 1];
strF(str, f);
next[0] = 0;
next[1] = 0;
int length = str.length();
for(int i = 2; i < length + 1; i++)
{
if(str[i - 1] != str[f[i] - 1])
{
next[i] = f[i];
}
else
next[i] = next [f[i]];
cout<<i<<endl;
}
delete [] f;
return next;

}
int main()
{
string str;
str = "aaaabfdsa";
int * next = new int [str.length() + 1];
next = strNext(str);
int length = str.length();
for(int i = 1; i < length + 1; i++)
cout << next[i] << " ";
cout << endl;
delete [] next;
}

展开全部

  • 写回答

1条回答 默认 最新

  • devmiao 2016-06-26 00:13
    关注
    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部