【问题标题】:Error "Abort signal from abort(3) (sigabrt) " for linked list in C++C++ 中链表的错误“来自 abort(3) (sigabrt) 的中止信号”
【发布时间】:2020-04-01 17:11:37
【问题描述】:

下面的代码是一个基本的循环链表,但是当输入一个较大的 n 值(例如 8 位)时,它会抛出“abort signal from abort(3) (sigabrt)”错误。我不确定这意味着什么,并且希望获得一些关于我的代码修复此问题的指导。 谢谢!

#include<bits/stdc++.h> 
using namespace std; 

//First I created a structure for a node in a circular linked list
struct Node 
{ 
    int data; 
    struct Node *next; 
}; 

// function to create a new node
Node *newNode(int data) 
{ 
Node *temporary = new Node; 
temporary->next = temporary; 
temporary->data = data; 
return temporary; 
} 

// This function finds the last man standing in
//the game of elimination
void gameOfElimination(int m, int n) 
{ 
    //first I created a circular linked list of the size which the user inputted 
    Node *head = newNode(1); 
    Node *prev = head; 
    //this loop links the previous node to the next node, and so on.
    for (int index = 2; index <= n; index++) 
    { 
        prev->next = newNode(index); 
        prev = prev->next; 
    } 
    prev->next = head; //This connects the last and first nodes in our linked list together. 

    //when only one node is left, which is our answer:
    Node *ptr1 = head, *ptr2 = head; 
    while (ptr1->next != ptr1) 
    { 

        int count = 1; 
        while (count != m) 
        { 
            ptr2 = ptr1; 
            ptr1 = ptr1->next; 
            count++; 
        } 

        /* Remove the m-th node */
        ptr2->next = ptr1->next; 
        ptr1 = ptr2->next; 
    } 

    printf ("%d\n ", 
            ptr1->data);
} 

//main program which takes in values and calls the function:
int main() 
{ 
    int n, p;
    cin>>n>>p;
    int m=p+1;
    gameOfElimination(m, n); 
    return 0; 
} 

【问题讨论】:

    标签: c++ c++11 pointers linked-list


    【解决方案1】:

    SIGABRT 通常在存在内存问题时发出(堆损坏很常见)。在您的代码中,我只看到调用了 new() 运算符,但您没有从链接列表中删除任何未使用的节点!似乎您正在耗尽分配给您的进程的内存。

    【讨论】:

      【解决方案2】:

      您可能内存不足。在程序执行期间检查您的 ram 使用情况,这可能会导致某些问题。

      【讨论】:

        【解决方案3】:
        enter code here
        
        #include<bits/stdc++.h>
        using namespace std;
        
        class Node{
         public: 
          int data;
          Node *next;
        
        };
        
        void traverse(Node *head)
        {
            while (head != NULL)
            {
                /* code */
                cout<<head->data<<"->";
                head = head->next;
            }
            cout<<"NULL"
        }
        
        int main()
        {
             Node *head = new Node();
             Node *second = new Node();;
             Node *third = new Node();;
             Node *fourth = new Node();;
             
             head->data = 5;
             head->next = second;
             //cout<<head->data;
             second->data=10;
             second->next=third;
        
             third->data = 15;
             third->next = fourth;
        
             fourth->data = 20;
             fourth->next= NULL;
        
            traverse(head);
        
             return 0;
        }```
        

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
        • 请添加一些细节以帮助其他人理解您的答案
        猜你喜欢
        • 1970-01-01
        • 2021-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多