SCC
SCC

Reputation: 720

BLOCK_TYPE_VALID error when calling delete [] on pointer to object

This Code returns an error on the delete [] placard_; call

void Protestor::destroy() { //Free's caller Protestor's dynamic memory
    delete [] placard_;
}

This code does not.

void Protestor::destroy() { //Free's caller Protestor's dynamic memory
    delete placard_;
}

This goes against my class notes, which state to ALWAYS call

delete []

rather than

delete

What is the explanation for this behaviour? Under what conditions must 'delete' be called instead of 'delete []'?

Here is the Definition for the Protester and Sign classes.

class Protester
{
  public:
    Protester(string name, string signSlogan, int signHeight, int signWidth,
              int rcmp_file = 0 );
    string getName() const;
    Sign getPlacard() const;
    void changePlacard( string newSlogan, int newHeight, int newWidth);
    void setRCMPfile(int RCMP_file);
    int getRCMPfile() const;

    //Big Three
    Protester(const Protester& other); //Copy Constructor
    ~Protester(); //Destructor
    Protester& operator= (const Protester& other); //Assignment Constructor


private:
    // name of the Protester
    string name_;

    // a sign the protester is wielding
    Sign* placard_;

    // the RCMP file number tracking this person (zero means no RCMP report)
    int rcmp_file_;

    //Big Three Helper Functions
    void copy(const Protester& other); //Performs Deep Copy of const Protester&
    void destroy(); //deletes [] placard_
                    //sounds better then cleanup, in my humblest of opinions.
};

class Sign
// a class representing information about signs/placards
{
public:
    // constructor to initialize sign text and dimensions
    Sign(string statement, int height, int width);

    // return sign text
    string getStatement() const;

    //return sign height
    int getHeight() const;

    //return sign width
    int getWidth() const;

    // change sign text
    void setStatement(string statement);

    // change sign dimensions
    void setSize(int height, int width);

private:
    // the text of the sign
    string statement_;

    // dimensions of the sign
    int height_;
    int width_;
};

Upvotes: 0

Views: 119

Answers (4)

hmjd
hmjd

Reputation: 122001

delete[] is called to free a dynamically allocated array: new type[]

delete is called to free a dynamically allocated object: new type

See delete C++ Wikipedia page.

Note, that if the destroy() function is invoked twice an attempt will be made to free an already deleted object as placard_ is not NULLed after the delete (if delete is called on a NULL pointer it has no effect).

Upvotes: 1

J.N.
J.N.

Reputation: 8431

This goes against my class notes, which state to ALWAYS call delete [] rather than delete

No that's wrong. You have to pair calls to new and delete and calls to new[] and delete[].

Note: But that's not how you should to that in modern C++. Use std::shared_ptr or std::unique_ptr instead. This is usually a much safer choice. Calls to new/new[] should be nearly always enclosed in smart pointer and delete should not be needed at all. Very few exceptions.

Upvotes: 1

cooky451
cooky451

Reputation: 3510

You use operator delete[] only when you're allocating with operator new[]. Furthermore, you should try to use containers (vector, list, ..) and smart pointers (unique_ptr, shared_ptr).

Upvotes: 0

Yochai Timmer
Yochai Timmer

Reputation: 49261

When you use new Object() you should use delete

When you use new Object[] (and array of objects) you should use delete[]

Upvotes: 1

Related Questions