簡體   English   中英

運行時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 cout << "inside trie insert" <<str<<endl; 是成功的。

第二個cout cout << "inside trie insert" <<str.substr(i)<< " " << str<<endl; 是看到錯誤的地方。

有人請幫我解決這個問題。

謝謝..

您遇到問題的原因是因為您已將child聲明為只有156元素的向量,但您在for循環中最多可以訪問256 這級聯一大堆潛在不確定的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM