A segmentation fault that won’t allow me to access a public variable.

huangapple go评论69阅读模式
英文:

A segmentation fault that won't allow me to access a public variable

问题

我正在编写一个模拟图书馆的程序,在其中的一个函数中遇到了段错误,我不明白为什么它不起作用。

void add()
{
    Book* newBook = new Book;
    Book* temp = new Book;
    temp = head;
    string tempstring;
    int id;
    int n;

    cout << "输入要添加的书籍的ID:";
    cin >> id;

    // 下面的if条件将检查链表是否为空,如果是,则仅添加书籍并退出函数
    if (head == NULL)
    {
        newBook->id = id;
        cout << "输入书籍的名称:";
        cin >> tempstring;
        newBook->nameOfTheBook = tempstring;
        cout << "输入作者的名称:";
        cin >> tempstring;
        newBook->author = tempstring;
        cout << "输入要添加的副本数量:";
        cin >> n;
        newBook->numOfBooks += n;

        newBook->next = NULL;
        head = newBook;
        return;
    }

    // 这里是出现错误的地方,条件应该是if(temp->id == id)
    while (temp != NULL)
    {
        if (temp->id == id)
        {
            temp->numOfBooks += 1;
            if (temp->avaliable == 0)
                temp->avaliable = 1;
            return;
        }
        temp = temp->next;
    }
    newBook->id = id;
    cout << "输入书籍的名称:";
    cin >> tempstring;
    newBook->nameOfTheBook = tempstring;
    cout << "输入作者的名称:";
    cin >> tempstring;
    newBook->author = tempstring;
    cout << "输入要添加的副本数量:";
    cin >> n;
    newBook->numOfBooks += n;

    // 这里temp的值应该是NULL,我用newBook替换了temp
    newBook->prev = temp->prev;
    newBook->next = NULL;
    temp->prev->next = newBook;

    newBook->avaliable = 1;
}
英文:

I'm writing a program that is supposed to simulate a library and in one of the functions I faced a segmentation fault and I don't see why it won't work.

void add()
{
    Book* newBook = new Book;
    Book* temp = new Book;
    temp = head;
    string tempstring;
    int id;
    int n;

    cout &lt;&lt; &quot;Enter the ID of the book you wanna add: &quot;;
    cin &gt;&gt; id;

in the if condition below will check if the linkedlist is empty or not and just add the book and get out of the function

    if(head == NULL)
    {
        newBook-&gt;id = id;
        cout &lt;&lt; &quot;Enter the name of the Book: &quot;;
        cin &gt;&gt; tempstring;
        newBook-&gt;nameOfTheBook = tempstring;
        cout &lt;&lt; &quot;Enter the Name of the Author: &quot;;
        cin &gt;&gt; tempstring;
        newBook-&gt;author = tempstring;
        cout &lt;&lt; &quot;Enter The number of copys you&#39;re going to add: &quot;;
        cin &gt;&gt; n;
        newBook-&gt;numOfBooks += n;

        newBook-&gt;next = NULL;
        head = newBook;
        return;
    }

here is where I have the error in the if condition if(temp->id == id)

if(

    while(temp != NULL)
    {
        if(temp-&gt;id == id)
        {
            temp-&gt;numOfBooks += 1;
            if(temp-&gt;avaliable == 0)
            temp-&gt;avaliable = 1;
            return;
        }
        temp = temp-&gt;next;
    }
    newBook-&gt;id = id;
    cout &lt;&lt; &quot;Enter the name of the Book: &quot;;
    cin &gt;&gt; tempstring;
    newBook-&gt;nameOfTheBook = tempstring;
    cout &lt;&lt; &quot;Enter the Name of the Author: &quot;;
    cin &gt;&gt; tempstring;
    newBook-&gt;author = tempstring;
    cout &lt;&lt; &quot;Enter The number of copys you&#39;re going to add: &quot;;
    cin &gt;&gt; n;
    newBook-&gt;numOfBooks += n;

here the value of temp should be NULL and I replaced the temp with the newBook

    newBook-&gt;prev = temp-&gt;prev;
    newBook-&gt;next = NULL;
    temp-&gt;prev-&gt;next = newBook;

    newBook-&gt;avaliable = 1;   
}

答案1

得分: 3

以下是翻译好的部分:

首先,在函数开头的这段代码:

        Book* newBook = new Book;
        Book* temp = new Book;
        temp = head;

产生了内存泄漏。类型为 `Book` 的对象被动态分配,其地址被赋给指针 `temp`,然后指针 `temp` 被重新赋值。因此分配的对象丢失。

还有另一个内存泄漏。动态分配的类型为 `Book` 的对象:

        Book* newBook = new Book;

可能因为这个 if 语句中的 `return` 语句而未被使用:

            if(temp->id == id)
            {
                temp->numOfBooks += 1;
                if(temp->avaliable == 0)
                temp->avaliable = 1;
                return;
            }

同样,分配的对象将会丢失。

至于程序崩溃,那么在 while 循环之后:

        while(temp != NULL)
        {
            if(temp->id == id)
            {
                temp->numOfBooks += 1;
                if(temp->avaliable == 0)
                temp->avaliable = 1;
                return;
            }
            temp = temp->next;
        }

指针 `temp` 等于 `NULL`。因此在这段代码中使用空指针:

        newBook->prev = temp->prev;
        newBook->next = NULL;
        temp->prev->next = newBook;

会引发未定义行为。

另外,当 `head` 等于 `NULL` 时,创建的节点的数据成员 `prev` 并未设置为 `NULL`(`nullptr`)。

你需要重新设计这个函数。
英文:

For starters this code snippet in the function beginning

    Book* newBook = new Book;
    Book* temp = new Book;
    temp = head;

produces a memory leak. An object of the type Book was dynamically allocated and its address was assigned to the pointer temp and then the pointer temp was reassigned. So the allocated object is lost.

And there is also one more memory leak. The allocated dynamically object of the type Book

    Book* newBook = new Book;

can be unused due to the return statement in this if statement

        if(temp-&gt;id == id)
        {
            temp-&gt;numOfBooks += 1;
            if(temp-&gt;avaliable == 0)
            temp-&gt;avaliable = 1;
            return;
        }

And again the allocated object will be lost.

As for the program crash then after the while loop

    while(temp != NULL)
    {
        if(temp-&gt;id == id)
        {
            temp-&gt;numOfBooks += 1;
            if(temp-&gt;avaliable == 0)
            temp-&gt;avaliable = 1;
            return;
        }
        temp = temp-&gt;next;
    }

the pointer temp is equal to NULL. So using the null pointer in this code snippet

    newBook-&gt;prev = temp-&gt;prev;
    newBook-&gt;next = NULL;
    temp-&gt;prev-&gt;next = newBook;

invokes undefined behavior.

Also when head is equal to NULL then the data member prev of the created node is not set to NULL (nullptr).

You need to redesign the function.

huangapple
  • 本文由 发表于 2023年5月21日 05:44:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定