首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++中链表出现错误"Abort signal from abort(3) (sigabrt)“

C++中链表出现错误"Abort signal from abort(3) (sigabrt)“
EN

Stack Overflow用户
提问于 2019-12-08 18:50:27
回答 2查看 341关注 0票数 1

以下代码适用于基本的循环链表,但当输入一个较大的n值(例如8位数)时,它会抛出"abort signal from abort(3) (sigabrt)“错误。我不确定这是什么意思,我想要一些关于我的代码修复这个问题的指导。谢谢!

代码语言:javascript
复制
#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; 
} 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-08 20:01:09

SIGABRT通常在存在内存问题时发出(堆损坏非常常见)。在您的代码中,我只看到new()操作符被调用,但是您没有从链表中删除任何未使用的节点!看起来您正在耗尽分配给您的进程的内存。

票数 1
EN

Stack Overflow用户

发布于 2019-12-08 19:49:32

你的内存可能会用完。检查你的程序执行过程中的内存使用情况,这可能会导致一些问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59234645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档