Seg Fault仅在每次运行中发生

时间:2017-08-31 00:31:23

标签: c++ c++11

这是我发布的第一个问题,我确保检查其他主题有类似主题和相同问题的帖子,但是迄今为止没有一个解决方案帮助过我。

问题:通过下面的makefile进行编译时

OBJ = hw1.o

hw1: $(OBJ)
    g++ -g -o hw1 $(OBJ)

hw1.o: 

./PHONE:
    clean

clean:
    rm hw1.o

每3次运行我收到一个分段错误。我在这个网站上看到的解决方案包括在主程序之外移动linked_list*个对象,但是当我尝试这个时,它每次运行都会给我分段错误。

以下是我的代码:

#include<iostream>
#include<iomanip>
#include<fstream>
#include<vector>

struct linked_list
{
    int data;

    void set_data(int d) { data = d; }
    int get_data(void) {return this->data; }
    void set_next(linked_list* ll) { this->next = ll; }
    linked_list* get_next(void) { return this->next; }

    linked_list* next;
};

//linked_list* head;    this is where I moved the objects out of main
//linked_list* tail;    which cause seg faults every run
//linked_list* temp;

void print_linked_list(linked_list*, int);

int main()
{
    linked_list* head;
    linked_list* tail;
    linked_list* temp;
    std::ifstream data1, data2;
    int element = 0;            //elements from list
    int size_ll = 0;

    //Open data1.txt and check for errors
    //data1.txt contains elements to be added in to both data structures
    data1.open("data1.txt");
    if(data1.fail())
    {
        std::cout << "Error: Couldn't open data1.txt\nCheck that file exists...\n";

        return 0;
    }

    //Open data2.txt and check for errors
    //data2.txt contains elements which need to be deleted in both data structures
    data2.open("data2.txt");
    if(data2.fail())
    {
        std::cout << "Error Couldn't open data2.txt\nCheck that file exists...\n";

        return 0;
    }

    head->set_next(NULL);
    tail->set_next(NULL);

    for(int i = 0; i <= 150; i ++)
    {
        data1 >> element;

        temp = new linked_list;
        temp->set_data(element);
        temp->set_next(NULL);

        if((size_ll % 50) == 0 && size_ll != 0)
        {
            std::cout << "\n\nPrinting current linked list (left to right, top to bottom):";
            std::cout << "\nCurrent size:   " << size_ll;
            std::cout << "\n\n";
            print_linked_list(head, 0);
            std::cout << "\n";
            std::cout << "End of current list.\n";
        }

        //handling linked list
        if(size_ll == 0)
        {
            head = tail = temp;

            size_ll++;
        }
        else
        {
            tail->set_next(temp);
            tail = temp;

            size_ll++;
        }
    }

    data1.close();
    data2.close();
}

//pass the next element of the linked list
void print_linked_list(linked_list* current, int lol)
{
    if(current == NULL)
    {
        return;
    }

    std::cout << std::setw(4) << current->get_data() << "  ";

    if(lol == 9)
    {
        std::cout << std::endl;
        lol = -1;
    }

    print_linked_list(current->get_next(), lol+1);
}

2 个答案:

答案 0 :(得分:1)

看看你写的是什么:

linked_list* head;
linked_list* tail;

然后你做:

head->set_next(NULL);
tail->set_next(NULL);

未初始化headtail。这是一个UB

答案 1 :(得分:1)

您没有初始化链接列表。如果你将它们作为静态变量,它们将被初始化为零,并且行head->set_next(NULL)将在尝试取消引用NULL时崩溃。

main()中定义它们时,它们具有随机堆栈值。如果堆栈值是无效地址,您将在同一行崩溃,但如果它恰好包含指向有效内存的地址,您只需开始覆盖任意内存。

相关问题