Etienne Berube
Etienne Berube

Reputation: 657

Setters with pointers in C++

I'm taking a C++ class and I'm having a hard time understanding when do I need to delete a pointer. From my previous understanding, we need to de-allocate memory whenever a pointer that was created with new is not used anymore. On the other hand, a pointer that is assigned using a reference to another variable doesn't need it.

In the case of the setter for an instance variable of a class, do we need it? There is no way to know if the pointer that is passed to the setter has been created with "new" or not.

Test.cpp

int main(){
string name = "john";
string address = "11 blv. hello";
string tel = "514-999-9999";
Date d(1,1,1);

Customer bob(name, address, tel, &d);

bob.printInfo();
Date d2(2,2,2);

bob.setDob(&d2); //Could also be new Date(2,2,2)

bob.printInfo();

return 0;
}

The setter in Customer.cpp:

void Customer::setDob(Date *d){

//delete(dob); Do I need to put it or not? 
Customer::dob = d;
}

Upvotes: 0

Views: 3495

Answers (1)

eerorika
eerorika

Reputation: 238381

I'm having a hard time understanding when do I need to delete a pointer.

From my previous understanding, we need to de-allocate memory whenever a pointer that was created with new is not used anymore.

Your understanding here is pretty much spot on. You need to delete a pointer exactly whenever you've allocated an object with a new expression, and you don't use it anymore.

Conversely, you must not delete anything that wasn't allocated with a new expression, and you must not delete anything that is still being used. Nor may you delete anything that has already been deleted.

On the other hand, a pointer that is assigned using a reference to another variable doesn't need it.

Correct. Not only isn't deleting a pointer to a variable object not needed, it is in fact something that musn't be done. Variables are not created with a new expression. As such, they must not be deleted.

In the case of the setter for an instance variable of a class, do we need it?

It depends on how that object was created. If it was created with new and if you're not going to delete it elsewhere, then you must delete it here. If the object was not created with new, or if you do delete it elsewhere, then you must not delete it here.

There is no way to know if the pointer that is passed to the setter has been created with "new" or not.

In that case, you must not delete the pointer here. Otherwise you could be deleting something that must not be deleted. In other words, the function must not take ownership of that pointer.

If the pointed object is dynamic, then it has to be deleted somewhere else:

// For demonstrative purpose only.
// It's never a good idea to use owning bare pointers.
// Use smart pointers.
auto ptr = new Date(2,2,2);
Date d2(2,2,2);
{
    Customer bob(name, address, tel, &d);
    bob.setDob(&d2); // OK
    bob.setDob(ptr); // OK
    bob.setDob(new Date(2,2,2)); // Not OK; would leak memory
}
delete ptr;

I see no good reason why a Customer class would point to an external Date object as the dob (date of birth?). That seems something that should be a sub-object of Customer.

Upvotes: 1

Related Questions