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.
source
share