error in class destructor

I just started my container class and already I'm having issues:

class Container
{
    private:

    string* BasePointer; // The starting pointer.
    unsigned int Capacity; // The number of values the container can hold.

    public:

    Container() // Default constructor.
    {
        Capacity = 1;
        BasePointer = new string[Capacity];
    }

    ~Container() // Destructor.
    {
        delete BasePointer; // Delete the container to prevent memory leaking.
    }
};

I get the error Container Classes(26467) malloc: *** error for object 0x100100088: pointer being freed was not allocated. What am I doing incorrectly?

Answers


The XXX ptr = new XXX[size] should be matched with array version delete [] ptr, not just regular delete.

Read about free store management in C++, and as James reminds us - follow the rule of three in cases like this.


Need Your Help

Core Data Model For Test Taking iOS App

ios objective-c core-data database-design database-schema

I'm building an iOS app for test taking and I want to be sure of my model before proceeding.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.