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 9286553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:26:43+00:00 2026-06-18T19:26:43+00:00

I have a class containing a member pointer which is dynamically allocated in its

  • 0

I have a class containing a member pointer which is dynamically allocated in its constructor as follows:

class Record {
public:
    Record(unsigned short numBytes, char* bufRecord);
    ~Record();

    unsigned short size() {return m_numBytes;}
private:
    unsigned short m_numBytes;
    char* m_bufRecord;
};

Record::Record(unsigned short numBytes, char* bufRecord) {
    m_numBytes = numBytes;
    m_bufRecord = new char[numBytes];

    for(unsigned short i=0; i<numBytes; i++)
        m_bufRecord[i] = bufRecord[i];
}

Record::~Record() {
    delete m_bufRecord;
}

It basically copies the input buffer into the dynamically allocated member buffer. I proceed to use this class as follows, in the constructor of another class:

class File {
public:
    File(const char* fileName);
    ~File();

    unsigned int numRecords() {return m_records.size();}
    Record getRecord(unsigned int numRecord) {return m_gdsRecords[numRecord];}
private:
    std::ifstream           m_file;
    std::vector<Record>     m_records;
};

File::File(const char* fileName) : m_file(fileName, ios::in | ios::binary) {
    while(!m_file.eof()) {
        char bufNumBytes[2];
        char* bufRecord;
        unsigned short numBytes;

        m_file.read(bufNumBytes, 2);
        numBytes = (bufNumBytes[0] << 8) + bufNumBytes[1] - 2;
        bufRecord = new char[numBytes];
        m_file.read(bufRecord, numBytes);

        Record record(numBytes, bufRecord);
        m_records.push_back(record);

        delete bufRecord;
    }
}

However, when I instantiate this class, I get the following error, which seems to state that I’m double-freeing the m_bufRecord:

*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001cb3280 ***

I’m guessing the problem lies with the insertion of a class containing a pointer to the vector element, and the destructor being called twice on the same pointer but I’m not sure how this happens. What am I doing wrong here?

  • 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-06-18T19:26:44+00:00Added an answer on June 18, 2026 at 7:26 pm

    This is a case of the Rule of three. If your class needs to free resources in the destructor, it generally needs to declare a copy constructor (and copy assignment operator), to either copy the owned resource, manage shared ownership or prevent being copied.

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

Sidebar

Related Questions

I have a class with a container (containing pointer) as a member: MyClass{ private:
I have a class in foo.h containing a static member class foo { public:
I have an example class containing two data points: public enum Sort { First,
I have a container class (containing a multi-index container) for which I have a
I have Value-Objects/Beans (only containing members, no logic): public class Parent { String first;
I have a Python class containing a list, to which I append() values. If
I have a class containing a set of C dynamic arrays which are all
I have a class containing a collection of items. For convenience I've provided GetCurrentItem
I have a class containing Linq To SQL objects that are used to populate
I have a class containing several properties (all are strings if it makes any

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.