SIGABRT program signal interrupted

My program has a structure

struct List      
{
    int data;
    List *next;
};

and the function of adding an item 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 talks about the problem

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:

List* add;
add = new List;

but got the same problem in the second line.

How to rewrite this? And is there a need to insert a function here that calls addL? Sorry, if this question has already been asked, I could not understand, looking through them.

+4
source share
1 answer

Either you have lost memory (perhaps your list is too large for your memory), or you are trying to find somewhere in memory that you are not allowed to.


Since the list is small, I suspect this is a problem (as indicated here ):

abort() , - . , malloc() abort(), .

: .

, , . , - .

+1

All Articles