2016-12-19 9 views
0

Iの値と次のノード(最終のノード内のポインタをnullptrことになっている)へのポインタを含むノードと一方向リストを作成しようとしています。予定通りセグメンテーション違反信号を

Howerver、物事が進んでいません。それは何の問題もなくコンパイルするのですが、私はそれを実行しようとすると、私は、この致命的なエラー状態を取得: SIGSEGV - セグメンテーション違反信号を。

それが使用する許可、または何かを持っていないメモリに到達しようとしていると思いますか?別の一般的な原因は、 "=="ではなく "="という偶発的なものですが、ここでは問題にはならないようです。

私がこのように、私のテストファイル内の任意のノードなしSorted_Listを構築しようとすると、エラーが発生しているようです:

:ここ
Sorted_List empty_list{}; 

が、私はエラーに関連することができます想像コードです

Sorted_List.cc

#include "Sorted_list.h" 
#include <iostream> 
#include <string> 
#include <sstream> 
using namespace std; 

Sorted_List::Sorted_List() : head{nullptr} {} 

Sorted_List::Sorted_List(initializer_list<int> i) 
    :Sorted_List() 
{ 
    for (auto ii : i) 
    { 
     add_val(ii); 
    } 
} 

Sorted_List::~Sorted_List() 
{ 
    if (!check_empty()) 
    { 
     Node* del = head; 
     while(del != nullptr) 
    { 
     Node* next = del->next; 
     delete del; 
     del = next; 
    } 
    } 
} 

bool Sorted_List::check_empty() const 
{ 
    return (head->value == 0 && head->next == nullptr); 
} 


void Sorted_List::del_val(int num) 
{ 
    Node* del = head; 
    if (num == 1) 
    { 
    head = del->next; 
    delete del; 
    } 
    for (int i = 1; i < num - 1; i++) 
    { 
     del = del->next; 
    } 
} 

void Sorted_List::add_val(int num) 
{ 
    Node* temp = new Node; 
    temp->value = num; 
    if (head == nullptr || head->value >= temp->value) 
    { 
     temp->next = head; 
     head = temp; 
    }  
    else 
    { 
     Node* current = head; 
     while(current->next != nullptr && current->next->value <temp->value) 
    { 
     current = current->next; 
    } 
     temp->next = current->next; 
     current->next = temp; 
    } 
} 
string Sorted_List::print(Sorted_List& list) 
{ 
    Sorted_List::Node* temp; 
    stringstream list_stream; 
    for(temp = list.head; temp != nullptr; temp = temp->next) 
    { 
     list_stream << temp->value; 
     if(temp->next != nullptr) 
    list_stream << ", "; 
    } 
    return list_stream.str(); 
} 

Sorted_List.h

#ifndef SORTED_LIST_H 
#define SORTED_LIST_H 

#include <string> 
#include <iostream> 
#include <initializer_list> 
#include <string> 

class Sorted_List 
{ 
private: 
    class Node 
    { 
    public: 
     int value{}; 
     Node* next{}; 
    }; 
Node* head{}; 

public: 
    Sorted_List(); 
    Sorted_List(std::initializer_list<int>); 
    ~Sorted_List(); 

    std::string print(Sorted_List&); 
    void add_val(int num); 
    bool check_empty() const; 
    void del_val(int num); 
}; 

#endif 

Sorted_List_test.cc

#define CATCH_CONFIG_MAIN 
#include "Sorted_list.h" 
#include "catch.hpp" 
#include <iostream> 
#include <string> 
using namespace std; 

TEST_CASE(" EMPTY ") 
{ 
    Sorted_List empty_list{}; // this is where the error occurs 
    //REQUIRE(empty_list.check_empty() == true); 
    //REQUIRE(empty_list.print(empty_list) == ""); 
} 

任意の手がかり?

+0

、このような問題を解決するために、エリックリペットで[小さなプログラムをデバッグする方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)をお読みください。 –

答えて

2

デバッガを使用すると、empty_listオブジェクトがが破棄されたときにクラッシュが発生することがわかります。より正確には、デストラクタから呼び出されたcheck_empty関数です。

これは、デフォルトのコンストラクタがheadをヌルポインタに設定してから、check_emptyにこのヌルポインタを逆参照するためです。 headがNULLポインタである場合

あなたcheck_empty機能がチェックする必要があります。将来的には

関連する問題