Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:29:24+00:00 2026-05-23T04:29:24+00:00

I have created a vector of class objects. The following program crashes with Pointer

  • 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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:29:25+00:00Added an answer on May 23, 2026 at 4:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a BST full of WordInfo objects that have a vector to
Storing objects in heterogeneous vector with stack-allocated objects Hello, Say I have an abstract
I have a class that creates a vector of objects. In the deconstructor for
I have the following class definition and it needs a copy constructor so deep
I have the following class: Class L{ public: bool foo(vector<bool> & data); private: C**
I have created a C# class file by using a XSD-file as an input.
I have a class called Movie with the following private data members: private: string
I am encountering the following bug. I have a class Foo . Instances of
I have a vector class with a properly overloaded Vect*float operator and am trying
I have a class called SparseMatrix which contain a private vector of type Cell.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.