Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between destroy the objects and free the associated memory

Tags:

c++

I have been diving into C++ primer 5th edition these days. I found that on the page 452-453, it says shared_ptr automatically destroys its objects and frees the associated memory. I don't quite understand it. So what's the difference between destroy the objects and free the associated memory?

like image 443
LiuHao Avatar asked Dec 20 '25 17:12

LiuHao


1 Answers

Consider an object of this class:

class foo {
    double* a;
    foo() { a = new double();}
    ~foo() {delete a;}
}

If you want to clean up after using such an object, it is not sufficient to free the memory of that object, but you have to call the destrcutor so that also a gets deleted properly.

like image 147
463035818_is_not_a_number Avatar answered Dec 22 '25 06:12

463035818_is_not_a_number



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!