簡體   English   中英

無法找出此內存錯誤(程序完成后崩潰)

[英]Can't figure out this memory bug (crash after program finishes)

整個程序: http//pastebin.com/x7gfSY5v

我制作了一個解析器,該解析器從.txt文件中獲取行,然后根據其值將它們放置在二進制搜索樹中。 ID是第一個字符串,值是后面的字符串,用/分隔

范例txt:

Abcd/foo/bar//    #ID/value/value2
Abce/foo2//       #ID/value

第一行將調用:

//parser
SequenceMap a;            #has id and value member variables
a.writeAcronym(id);       #Abcd
a.writeValue(value);      #foo
insertNode(a, root);      #Abcd/foo put into tree
a.writeValue(value2);     #bar
insertNode(a, root);      #Abcd/bar put into tree

創建樹本身不會導致崩潰,並且可以正常工作。 只有當我嘗試在樹崩潰時訪問樹的最小值時,才可以。 盡管它仍然顯示正確的最小值。

這是獲得最小值的函數,我懷疑這是問題的根本原因。

但是在main()中運行它會使我的程序崩潰:

    string printMin(Node * x)
    {
        if (x == nullptr)
            return nullptr;       //this isn't ever called, I parse the tree before calling this fucntion
        if (x->left == nullptr)
            return x -> sqmap.getAcronym();  //returns correct value (end of program) then crash
        else
            printMin(x->left);
    }

    void printMain(){
        cout << printMin(root) << endl;
    }

主要():

a.parse("foo.txt");        //doesn't crash
a.printMain();             //crashes when called
                           //but doesn't crash if I remove both a.writeValue(value) from parser

這是插入/解析器:

    void parse(string file)         //uses delimiter to split lines
    {
        string line, id, value, value2;
        ifstream myfile(file);

        if (myfile.is_open())
        {
            while(getline(myfile, line))
            {
                if (!line.empty())
                {
                    SequenceMap a;
                    istringstream is(line);
                    getline(is, id, '/');
                    a.writeAcronym(id);
                    getline(is,value, '/');
                    a.writeValue(value);        //program doesn't crash if I remove this

                    insertNode(a, root);

                    getline(is,value2,'/');                

                    if (value2 != "")
                    {
                        a.writeValue(value2);   //program doesn't crash if I remove this
                        insertNode(a, root);
                    }
                }
            }
        myfile.close();
        }
        else
            cout << "Could not open file.";
    }

/*****************************************************/

    void insertNode(SequenceMap & sq, Node * & x)
    {
        if (x == nullptr)
            x = new Node(sq, nullptr, nullptr);
        else if (sq.getValue() < x->sqmap.getValue())
            insertNode(sq, x->left);
        else if (sq.getValue() > x->sqmap.getValue())
            insertNode(sq, x->right);
        else if (sq.getValue() == x->sqmap.getValue())
            x->sqmap.Merge(sq);
    }

我現在嚴重地陷入困境,我唯一的猜測是它是一個內存錯誤。 一切正常,我得到正確的最小值。 但是,在最后一行代碼結束之后,我立即崩潰了。 我為樹創建了一個析構函數,但是並沒有解決。 誰能解決這個問題?

遍歷左節點時, printMin不會返回值,因此會從該值返回垃圾值,而不是string 如果您一直使用警告進行編譯,則應該收到有關該警告的警告。

暫無
暫無

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

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