C++中链表的错误"Abort signal from abort(3) (sigabrt) "
Error "Abort signal from abort(3) (sigabrt) " for linked list in C++
以下代码用于基本的循环链表,但是当输入n(例如8位数字(的大值时,它会抛出"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 ("%dn ",
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;
}
SIGABRT通常在存在内存问题时发出(堆损坏很常见(。在您的代码中,我只看到正在调用的 new(( 运算符,但您没有从链表中删除任何未使用的节点!似乎您正在耗尽分配给进程的内存。
您可能内存不足。在程序执行期间检查您的 ram 使用情况,这可能会导致某些事情。
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;
}```
相关文章:
- 中止信号来自 C++ 中的中止(3) (SIGABRT)
- 在macOS Mojave上尝试OpenCV视频捕获时"Abort Trap: 6"
- 为什么我在此代码中收到 SIGABRT 错误
- OpenCL 内核计时测量 0 秒或导致 SIGABRT
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- SIGABRT 和线程相关的异常,但在调试期间工作正常
- C++ abort() 在函数内的抽象类对象指针调用上
- SIGABRT 在线程中访问内存时
- EXC_BAD_ACCESS或SIGABRT,同时从C++拨打scipy.stats.anderson_ksamp
- 程序在VS2017上以"abort() has been called"错误结束
- 当我的代码工作正常时,为什么在SPOJ中遇到运行时(Sigabrt)错误
- SIGABRT:使用斯托伊/斯托尔时超出范围
- 相机崩溃:带JNI代码的致命信号6(Sigabrt)
- 在SIGABRT c ++信号之后继续运行程序
- CPP中的Sigabrt错误
- 将矢量<int>推入矢量<矢量<int>>时,SIGABRT - free():下一个大小无效(快速)
- std::shared_ptr的擦除导致SIGABRT
- std::unique_ptr 在虚拟析构函数上重置 SIGABRT
- 在数组中移动元素 SIGABRT 错误
- 如何防止在分配整数数组时发生 SIGABRT