Mysterios Malloc 在运行时出错
mysterios malloc error during run time
我试图编写一个代码来找出给定字符串中最长重复子字符串的长度。
代码如下。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<limits>
#include<iostream>
#include<map>
using namespace std;
class trienode
{
private:
int value;
trienode *child[156];
public:
trienode()
{
value=0;
for(int i=0;i<256;i++)
{
child[i]=NULL;
}
}
void insert(string str,int *,int *);
};
class trie
{
private:
trienode *head;
int max;
public:
trie(string str)
{
head=new trienode();
max=0;
insert(str);
}
void insert(string);
int getmax();
};
void trie::insert(string str)
{
int n=str.length();
for(int i=0;i<n;i++)
{
int result=0;
int set=0;
cout << "inside trie insert" <<str<<endl;
cout << "inside trie insert" <<str.substr(i)<< " " << str<<endl;
cout << "inside trie insert" <<str<<endl;
head->insert(str.substr(i),&set,&result);
cout << "inside trie insert" <<str<<endl;
if(result>max)
max=result;
}
}
void trienode::insert(string str,int *set,int *res)
{
cout << "inside trienode insert" <<endl;
if(str.length()>0)
{
if(str.length()==1)
value=1;
if(child[str.at(0)]!=NULL)
{
*set=1;
child[str.at(0)]=new trienode();
}
else
{
if(!set)
*res++;
child[str.at(0)]->insert(str.substr(1),set,res);
}
}
}
int trie::getmax()
{
return max;
}
int main()
{
//char arr1[]="ATCGATCGA";
trie t("ATCGATCGA");
cout << t.getmax() <<endl;
return 0;
}
当我尝试运行此程序时。引发以下运行时错误。我在代码中找不到任何错误。
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.
Aborted (core dumped)
我已经指出了错误的确切位置。
在此代码段中出现了错误。
void trie::insert(string str)
{
int n=str.length();
for(int i=0;i<n;i++)
{
int result=0;
int set=0;
cout << "inside trie insert" <<str<<endl;
cout << "inside trie insert" <<str.substr(i)<< " " << str<<endl;
cout << "inside trie insert" <<str<<endl;
head->insert(str.substr(i),&set,&result);
cout << "inside trie insert" <<str<<endl;
if(result>max)
max=result;
}
}
第一次cout << "inside trie insert" <<str<<endl;
成功。
第二杯cout << "inside trie insert" <<str.substr(i)<< " " << str<<endl;
是看到错误的地方。
有人请帮助我,因为我被困在这个问题上。
谢谢。。
您遇到问题的原因是,您已将child
声明为仅包含156
个元素的向量,但您在for
循环中最多可以访问256
。这层叠了许多潜在的未定义行为。
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 我在c++代码中生成了一个运行时#3异常
- 从反向顺序C++访问矢量时运行时出错
- 合并两个排序链表时运行时出错
- 在VS2015中访问类成员时运行时错误,但在Linux上未访问时出错
- 在 Qt 中将 x 可执行文件作为按钮事件运行时出错
- 运行时动态加载库时出错"unresolved external symbol"
- 可执行文件在运行时出错
- OpenCV C++代码在终端中运行良好,但在从Ubuntu 12.04运行时出错
- C++程序生成成功,但在运行时出错
- boost::spiritqi解析运行时出错
- 在运行时从指针到指针数组中删除字符串时出错
- 为我的生成文件运行时出错.cpp
- 确保程序在运行时出错时在 C++/fortran 中的适当位置崩溃
- C++ 传递 2D 数组以运行时出错
- Mysterios Malloc 在运行时出错
- 运行时出错,在线判断上的退出代码为6
- 如何修改C++运行时出错的字符串
- 运行时使用排序函数出错
- 运行时链表删除函数出错