簡體   English   中英

析構函數中的分段超限故障

[英]Segmentation out-of-range fault in destructor

我的析構函數中存在分段錯誤,但我不確定為什么。 該代碼用於鍵/值對的映射,該映射存儲在Node數組中並鏈接在一起以避免沖突。

template<class V>
map<string, V>::~map()
{
    for(unsigned int i = 0; i < SIZE; i++){
        if(hashArray[i] != NULL){
            Node* tmpNode = hashArray[i];
             Node* currentNode = hashArray[i];
            while(currentNode->next != NULL){
                currentNode = currentNode->next;
                delete tmpNode;
                tmpNode = currentNode;
            }
            delete tmpNode;
        }
    }

    delete [] hashArray;
}

調試器指向此行,但我確定我不會超出范圍。

while(currentNode->next != NULL){

如果需要,可以提供任何其他代碼。 預先感謝您的幫助。 :)

我通過消除重復的hashArray[i]對其進行了一些清理。 還擺脫了對null的重復檢查:

template<class V>
map<string, V>::~map()
{
    for(unsigned int i = 0; i < SIZE; i++) {
        Node* currentNode = hashArray[i];
        while(currentNode) {
            Node* next = currentNode->next;
            delete currentNode;
            currentNode = next;
        }
    }

    delete [] hashArray;
}

暫無
暫無

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

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