类对象的向量

发布于 2024-11-14 23:22:00 字数 2329 浏览 3 评论 0原文

我创建了一个类对象向量。以下程序崩溃,

 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事还在继续 2024-11-21 23:22:00

我没有详细查看你的代码,但如果你认为你需要一个复制构造函数,那么你还需要一个赋值运算符。而且,为什么,为什么,为什么要写这些东西?为什么不使用 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.

难如初 2024-11-21 23:22:00

使用 delete[] 而不是 delete,因为您要释放数组。

顺便说一句,在 main() 中,您还应该调用 delete[] test;,否则,您将遇到内存泄漏。

Use delete[] instead of delete, since you are freeing an array.

By the way, in main(), you should also invoke delete[] test;, otherwise, you'll get a memory leak.

诠释孤独 2024-11-21 23:22:00

一般来说,我会避免使用带有指针的对象作为成员(例如 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文