KevinCamacho
KevinCamacho

Reputation: 39

Do I need to delete current member data when overriding copy/move assignment overload?

I am studying for a final tomorrow in a c++ class, and one thing that I was thinking about while studying was what happens to assigned-to object data after a copy/move assignment overload is called.

Say we have this class:

class linked_list {

public:
    //You need to implement this part.
    int num_nodes;
    node * head;

linked_list() { num_nodes = 0; head = nullptr; }

~linked_list(); //dectructor
linked_list(const linked_list &other); //copy constructor
void operator=(linked_list &other); //copy assignment overload
linked_list(linked_list &&other); //move constructor
void operator=(linked_list &&other); //move assignmend overload

};

I realized that throughout the whole semester I've been just copying in/taking ownership of the other data, without ever worrying about the previous data in the assigned-to object.

void linked_list::operator=(linked_list &other) { //copy assignment overload
    num_nodes = 0;
    head = nullptr;
    node * n = other.head;
    while (n != nullptr) {
        push_back(n->value);
        n = n->next;
    }
}

void linked_list::operator=(linked_list &&other) { //move assignment overload
    num_nodes = other.num_nodes;
    head = other.head;
    other.head = nullptr;
}

With the two examples that I have here, wouldn't the data of all the nodes in the current (this) linked_list still be floating around in memory? Before doing my copying/moving, would I have to iterate through the entire current linked list and delete each node?

For some reason I get the vibe from both other students and the course material that the class destructor is implicitly called before entering the two overloaded functions, because NOWHERE do I ever see any mention about having to free up current data first.

Any clarification would be extremely helpful.

Thank you,

Upvotes: 1

Views: 364

Answers (2)

AnT stands with Russia
AnT stands with Russia

Reputation: 320421

Your concerns are justified. Yes, every time you implement assignment operator (copy or move or any other kind), you are responsible for proper deallocation of "old" data potentially present in (and owned by) the object before copying/moving the new data in.

One can roughly describe the functionality of assignment operator as a combination of destructor call followed by copy/move constructor call (don't take it literally: this is not a good way to implement the assignment operator). The destruction of "old" data is not done automatically for you, i.e. there's no automatic implicit destructor call before invocation of your assignment operators, contrary to the vibe you say you got from the course material. It is your responsibility to manually implement the destruction of "old" data.

Take a look Copy-And-Swap idiom as one possible idiomatic way to implement assignment operators.

Of course, all this applies only if your class's members don't handle the proper assignment semantics themselves (in accordance with the Rule of Zero). In your example you are using owning raw pointers, meaning that it is your responsibility to perform the required resource management steps manually.

Upvotes: 3

Loki Astari
Loki Astari

Reputation: 264381

With the two examples that I have here, wouldn't the data of all the nodes in the current (this) linked_list still be floating around in memory?

Yes. You need to make sure the old data is either re-used/delete or accounted for in some fashion.

Before doing my copying/moving, would I have to iterate through the entire current linked list and delete each node?

That is one technique. I would look up the copy and swap idiom as this may help you a bit in this situation.

For some reason I get the vibe from both other students and the course material that the class destructor is implicitly called before entering the two overloaded functions.

No this does not happen.

because NOWHERE do I ever see any mention about having to free up current data first.

You absolutely must do something with the data you can not just let it leak. But that does not necessarily mean you want to directly free it (it can potentially be re-used). If you use the copy and swap idiom then there is a sort of re-use and destruction based on the copied object.

Any clarification would be extremely helpful.

Hope that helps somewhat.

Upvotes: 2

Related Questions