类对象的向量
我创建了一个类对象向量。以下程序崩溃,
"Pointer being freed was not allocated".
我也进行了深度复制。我看不出双重删除发生在哪里。我缺少什么?
#include <iostream>
#include <vector>
using namespace std;
enum MessageType { HEADER, DATA, CLOSE};
typedef class bufferElement{
public:
char *buffer ; //The actual data
int64_t length; //length of the data
MessageType messagetype;
/**
* Copy constructor for the structure
*/
bufferElement(const struct bufferElement &toCopy)
{
std::cout << "Copying the buffer vector - Copy Constructor for buffer" << std::endl;
buffer = new char[toCopy.length];
memcpy(buffer,toCopy.buffer,toCopy.length);
length = toCopy.length;
messagetype = toCopy.messagetype;
}
bufferElement()
{
buffer = NULL;
length =0;
messagetype = HEADER;
}
/**
* Initialises the vector element
* @param messagetype
* what type of message is the particular element.
* @param element
* The buffer element
* @param length_t
* The length/size of the buffer element
*/
bufferElement(char *element, int64_t length_t, MessageType messagetype_t) //constructor
{
std::cout << "The buffer element is Initialized" << std::endl;
buffer = new char[length_t];
messagetype = messagetype_t;
length = length_t;
memcpy(buffer, element, length_t);
}
~bufferElement()
{
std::cout << "Freeing the buffer in the vector - Destructor" << std::endl;
delete buffer;
buffer = NULL;
}
} messageHolder;
int main()
{
vector<messageHolder> v;
for(int64_t i=0; i< 1000000000000000000; i++)
{
int size = rand()%10000+5;
char *test = new char[size];
messageHolder m(test, size, HEADER );
v.push_back(m);
if(rand()%3)
{
v.erase(v.begin());
}
}
return 0;
}
I have created a vector of class objects. The following program crashes with
"Pointer being freed was not allocated".
I have deep copied as well. I don't see where the double delete is happening. What am I missing ?.
#include <iostream>
#include <vector>
using namespace std;
enum MessageType { HEADER, DATA, CLOSE};
typedef class bufferElement{
public:
char *buffer ; //The actual data
int64_t length; //length of the data
MessageType messagetype;
/**
* Copy constructor for the structure
*/
bufferElement(const struct bufferElement &toCopy)
{
std::cout << "Copying the buffer vector - Copy Constructor for buffer" << std::endl;
buffer = new char[toCopy.length];
memcpy(buffer,toCopy.buffer,toCopy.length);
length = toCopy.length;
messagetype = toCopy.messagetype;
}
bufferElement()
{
buffer = NULL;
length =0;
messagetype = HEADER;
}
/**
* Initialises the vector element
* @param messagetype
* what type of message is the particular element.
* @param element
* The buffer element
* @param length_t
* The length/size of the buffer element
*/
bufferElement(char *element, int64_t length_t, MessageType messagetype_t) //constructor
{
std::cout << "The buffer element is Initialized" << std::endl;
buffer = new char[length_t];
messagetype = messagetype_t;
length = length_t;
memcpy(buffer, element, length_t);
}
~bufferElement()
{
std::cout << "Freeing the buffer in the vector - Destructor" << std::endl;
delete buffer;
buffer = NULL;
}
} messageHolder;
int main()
{
vector<messageHolder> v;
for(int64_t i=0; i< 1000000000000000000; i++)
{
int size = rand()%10000+5;
char *test = new char[size];
messageHolder m(test, size, HEADER );
v.push_back(m);
if(rand()%3)
{
v.erase(v.begin());
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有详细查看你的代码,但如果你认为你需要一个复制构造函数,那么你还需要一个赋值运算符。而且,为什么,为什么,为什么要写这些东西?为什么不使用 std::string ?您觉得需要编写自己的浮点类型吗?不,我不这么认为。
I haven't looked at your code in detail, but if you think you need a copy constructor, you also need an assignment operator. And, why, why, why write this stuff anyway? Why not use std::string? Do you feel the need to write your own floating point types? No, I didn't think so.
使用
delete[]
而不是delete
,因为您要释放数组。顺便说一句,在
main()
中,您还应该调用delete[] test;
,否则,您将遇到内存泄漏。Use
delete[]
instead ofdelete
, since you are freeing an array.By the way, in
main()
, you should also invokedelete[] test;
, otherwise, you'll get a memory leak.一般来说,我会避免使用带有指针的对象作为成员(例如 bufferElement 中的 buffer)作为向量(或任何其他 stl 容器)中的类型,而无需复制构造函数和赋值运算符。如果析构函数释放该指针,那么就会导致破坏。你应该八个
a) 提供复制ctr和赋值操作
或
b)不要使用指针(使用智能指针,如 boost::scoped_ptr 或 boost:shared_ptr ,甚至在你的情况下使用 auto_ptr ),我更喜欢
In general I would avoid using objects with pointers as members (like buffer in your bufferElement) as types in vector (or any other stl container for that matter) without a copy constructor AND an assignment operator. If the destructor frees that pointer it's a recipe for destruction. You should eighter
a) provide both the copy ctr and the assignment op
OR
b) don't use pointers (use smart pointers like boost::scoped_ptr or boost:shared_ptr or even in your case auto_ptr) which I favor more