(C++ 错误:未分配正在释放的指针)为链表
(C++ Error: pointer being freed was not allocated) for linked lists
有了这个主:
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;
}
}
以及类队列中的这些函数定义,该类队列具有链表,其中"头"和"尾"作为指向链表中第一个和最后一个节点的指针,每个节点都包含另一个结构"data",该结构存储一个重新定义的类型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;
}
为什么我会收到此错误?4装配线模拟器(9701) malloc: * 对象 0x1001000e0 的错误:未分配正在释放的指针* 在malloc_error_break中设置断点进行调试
有了您展示的代码,我认为问题是您将队列的副本q
传递给函数print_first
。
void print_first(Queue q)
如果您没有正确编写复制构造函数,那么该函数可能会遇到一些问题。
相关文章:
- 正在理解智能指针,但出现错误:未分配正在释放的指针
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- 如何在向量中释放指针?
- 在C++中释放内存期间,迭代器与指针有何不同
- 包含矢量指针的结构的内存释放问题
- 错误:malloc:对象 0x7f9edf504080 的 *** 错误:未分配正在释放的指针
- 错误:释放指针未分配在矢量模板类C 中
- 如何在指针初始化为数组的第一个索引后释放指针
- 如何释放指针树中的所有内存
- 从 std::align 中释放指针
- C :释放指针没有分配在课堂上
- 构造向量 C++ 后无法释放指针
- nullptr是否释放指针的内存
- C/C++ 通过调用引用或直接释放指针
- 分段释放指针向量的错误
- 有效地从指向对象本身释放指针
- 如何释放指针向量中的元素
- Sqlite c/c++api,负责从sqlite3_column_text中释放指针
- 释放指针后分配给指针
- 正在释放指针