[英]pointer being freed was not allocated for pointer assignment
I was trying to change a ListNode struct into a class format but am running into some issues when testing it. 我试图将ListNode结构更改为类格式,但在测试时遇到了一些问题。
Getting a.out(7016) malloc: * error for object 0x7fff65333b10: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug 获取a.out(7016)malloc: *对象0x7fff65333b10的错误:未释放指针*在malloc_error_break中设置断点以进行调试
chainLink.hpp
#ifndef CHAINLINK_H
#define CHAINLINK_H
using namespace std;
#include <iostream>
#include <cstdlib>
template <typename Object>
class chainLink
{
private:
Object storedValue;
chainLink *nextLink;
public:
//Constructor
chainLink(const Object &value = Object()): storedValue(value)
{
nextLink = NULL;
}
/* Postcondition: returns storedValue;
*/
Object getValue()
{
return storedValue;
}
/* Postcondition: sets storedValue = value
*/
void setValue(Object &value)
{
storedValue = value;
}
/* Postcondition: sets nextLink to &value
*/
void setNextLink(chainLink* next)
{
nextLink = next;
}
chainLink* getNext()
{
return nextLink;
}
~chainLink()
{
delete nextLink;
}
};
#endif
My test file, assume includes 我的测试文件,假设包括
int main()
{
chainLink<int> x(1);
cout << "X: " << x.getValue() << " "<< endl;
chainLink<int> y(2);
cout << "Y: " << y.getValue() << " "<< endl;
chainLink<int>* z = &y;
cout << &y << " " << z << endl;
x.setNextLink(z);
}
OUTPUT: X: 1 Y: 2 0x7fff65333b10 0x7fff65333b10 a.out(7016) malloc: * error for object 0x7fff65333b10: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug Abort trap: 6 输出:X:1 Y:2 0x7fff65333b10 0x7fff65333b10 a.out(7016)malloc: *对象0x7fff65333b10的错误:未分配的指针*在malloc_error_break中设置断点以调试中止陷阱:6
The error seems to be thrown by the setNextLink function. setNextLink函数似乎抛出了错误。
Any help greatly appreciated. 任何帮助非常感谢。
In the last line of main
you are calling setNextLink
with a pointer to an object with automatic storage duration ( z
holds the address of y
). 在main
的最后一行,您使用指向具有自动存储持续时间的对象的指针调用setNextLink
( z
保存y
的地址)。 Your list tries to delete that pointer when it is destroyed, hence the error ( y
has not been allocated dynamically, and hence cannot be deleted dynamically). 您的列表尝试在销毁时删除该指针,因此错误( y
尚未动态分配,因此无法动态删除)。
You are giving setNextLink
a pointer to an automatically allocated variable, 你给setNextLink
一个指向自动分配变量的指针,
x.setNextLink(z); // z points to automatic object y
which you attempt to delete in the constructor. 您尝试在构造函数中删除它。
~chainLink() {
delete nextLink; // attempts to delete automatic object y
}
You need to pass a pointer to a dynamically allocated object, or make your own insde your chainLink
class. 您需要将指针传递给动态分配的对象,或者让您自己的指向您的chainLink
类。
Note : In C++, struct
s and class
es are the same bar some differences . 注意 :在C ++中, struct
s和class
es是相同的一些区别 。 A equivalent types can be implemented using either of the two. 可以使用两者中的任何一个来实现等效类型。
After the line x.setNextLink(z);
行x.setNextLink(z);
x.nextLink
points to z
, which in turn is pointing to y
. x.nextLink
指向z
,而z
又指向y
。 But y
is a local object . 但是y
是一个本地对象 。 It is allocated on stack and not the heap. 它分配在堆栈而不是堆。 So it is illegal to call delete
on it. 因此,在其上调用delete
是违法的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.