Aagman
Aagman

Reputation: 704

Calling delete[] inside destructor in c++

I Have a doubt in the following code, there is a destructor delete line[] inside the destructor, I just want to know if there any stack over flow for this delete which can be a result of recursive call to destructor.

class Line {
public:
    char *line;
    Line(const char *s = 0) {
        if (s) {
            line = new char[strlen(s)+1];
            strcpy(line, s);
        } else {
            line = 0;
        }
    }

    ~Line() {
        delete[] line; //----------> how this delete will work?
        line = 0;
    }

    Line &operator=(const Line &other) {
        std::cout <<"go"<< endl;
        delete[] line; //----------> purpose of using this delete??
        line = new char[other.len()+1];
        strcpy(line, other.line);
        return *this;
    }

    int operator<=(const Line &other) {
        int cmp = strcmp(line, other.line);
        return cmp <= 0;
    }

    int len() const {
        return strlen(line);
    }
};





int main() {
Line array[] = {Line("abc"), Line("def"),
                Line("xyz")};
   Line tmp;
  }

The delete inside the overloaded assignment operator is to clean the memory before assigning new memory(I have read it somewhere, correct me if I am wrong), but does this delete will call the destructor?

Please Explain

Upvotes: 2

Views: 564

Answers (4)

Rishikesh Chandra
Rishikesh Chandra

Reputation: 691

C++ by default takes care of delete[] for char*, therefore you do not need to do anything.

Upvotes: 1

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 123439

The argument to delete[] is a char*, ie there is no destructor called (and also there is no recursive calling of the destructor).

If you had a destructor like this:

 ~Line() { delete this; }   // DONT DO THIS !!! (also for other reasons it is not OK at all)

this would attempt to call itself recursively, but your code looks fine.

In the assignment operator

line = new char[other.len()+1];

will allocate new memory and assign a pointer (pointing to this memory) to line. This will cause that you have no handle to the old memory anymore and to avoid a leak you need to delete it before.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234875

delete[] line; pairs the new char[strlen(s)+1]; statements in the constructor and assignment operator. Note that delete[] line; is a no-op if line is set to nullptr, which is what the else branch assignment does, although it sloppily uses 0 in place of nullptr.

Be assured the destructor is not called recursively. It's just that the destructor is used to release any allocated memory.

But using std::string line; as the class member variable, or perhaps even the entire class itself would be far, far easier. There are some subtle bugs in your code - self assignment being one of them, and the copy constructor is missing. Let the C++ Standard Library take care of all this for you. In short you could write

int main() {
    std::string array[] = {"abc", "def", "xyz"};
    std::string tmp;
}

Upvotes: 1

Erik
Erik

Reputation: 153

No, it will not.

This delete statement will delete the char array. The destructor of Line is only called when destroying the Line object. That is however not the case here.

The variable line and the object/class Line are different things.

The line variable is a member-variable in the Line class. So those two names seem the same but are completly different.

Upvotes: 2

Related Questions