Reputation: 1256
With this main:
void print_first(Queue q);
int main()
{
Queue q1;
Queue q2(4);
q2.push(5);
q2.push(3);
// q2.print_list();
for (int i = 0; i < 3; i++)
{
print_first(q2);
}
return 0;
}
void print_first(Queue q)
{
if (!q.isEmpty())
{
cout << q.pop() << endl;
}
else
{
cout << "Nothing in queue" << endl;
}
}
and these function definitions in a class Queue which has a linked lists with "head" and "tail" as pointers to the first and last Nodes in a linked list, each node which contains another struct "data" which stores an redefined type ElementType:
bool Queue::push(ElementType newElement)
{
tail->next = new Node;
if (tail->next == NULL)
{
return false;
}
tail->next->data.value = newElement;
tail->next->next = NULL;
tail = tail->next;
return true;
}
ElementType Queue::pop()
{
Node *newNode;
ElementType returnData;
returnData = head->data.value;
newNode = head;
head = head->next;
delete newNode;
return returnData;
}
bool Queue::isEmpty()
{
if(head == NULL)
{
return true;
}
return false;
}
why am I getting this error? 4 Assembly Line Simulator(9701) malloc: * error for object 0x1001000e0: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug
Upvotes: 1
Views: 927
Reputation: 310990
Having the code you showed I think that the problem is that you pass a copy of the queue q
to function print_first
.
void print_first(Queue q)
If you did not write correctly the copy constructor then you can have some problems with the function.
Upvotes: 1