C++ 程序收到信号 SIGABRT,中止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29459957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Program received signal SIGABRT, Aborted
提问by Tami
There is a structure in my program
我的程序中有一个结构
struct List
{
int data;
List *next;
};
and a function of adding an element to the tail of the list:
以及向列表尾部添加元素的函数:
void addL(List* &tail, int dat)
{
if (tail==NULL)
{
tail = new List;
tail->data = dat;
tail->next=NULL;
}
else
{
tail->next = new List;
tail = tail->next;
tail->data = dat;
tail->next = NULL;
}
}
gdb says about the problem
gdb 说明了这个问题
terminate called after throwing an instance of 'St9bad_alloc'
what(): std::bad_alloc
Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
in line
排队
tail->next = new List;
I tried to make another variable of type List like that:
我试图制作另一个类似 List 类型的变量:
List* add;
add = new List;
but got the same problem in second line.
但在第二行遇到了同样的问题。
How to rewrite this correctly? And is it any need to paste here the function which calls addL? Sorry, if this question had already been asked, I could not understand while looking through them.
如何正确重写?是否需要在此处粘贴调用 addL 的函数?对不起,如果这个问题已经被问过了,我在翻阅它们时无法理解。
回答by gsamaras
Either you are out of memory (maybe your list is too big for your memory) or you are trying somewhere in the memory that you are not allowed to.
要么您内存不足(可能您的列表对于您的记忆来说太大了),要么您正在尝试内存中不允许的某处。
Since the list is small, then I suspect this is the issue (as stated here):
由于名单是小的,那么我怀疑这是问题(如说这里):
abort()
is usually called by library functions which detect an internal error or some seriously broken constraint. For examplemalloc()
will callabort()
if its internal structures are damaged by a heap overflow.
abort()
通常由检测内部错误或某些严重破坏的约束的库函数调用。例如,如果其内部结构因堆溢出而损坏,malloc()
则调用abort()
。
Another relevant question lies here.
另一个相关问题就在这里。
So I suggest you take a piece of paper and a pen and draw what your code does. There is probably a tangling pointer or something.
所以我建议你拿一张纸和一支笔画出你的代码的作用。可能有一个缠结的指针什么的。