简体   繁体   English

程序接收信号SIGABRT,已中止

[英]Program received signal SIGABRT, Aborted

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? 是否需要粘贴调用addL的函数? Sorry, if this question had already been asked, I could not understand while looking through them. 对不起,如果这个问题已经被问过,我一看就知道了。

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. abort()通常由库函数调用,它检测内部错误或严重破坏的约束。 For example malloc() will call abort() if its internal structures are damaged by a heap overflow. 例如,如果内部结构被堆溢出损坏, 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. 可能有一个纠结的指针或东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM