« first day (733 days earlier)      last day (2344 days later) » 

06:31
Is my summary correct? :

use `delete` -> definitely calls a object's destructor(for class type)
when a object's destructor executes -> it's not necessary for the constuctor to use `delete` if not specified intentionally
I am reading C++ Primer and the book does not give a similar conclusion. So based on my own understanding, wanna ask if my summary is correct. :D
nwp
nwp
07:17
You can call delete on a nullptr which doesn't call a destructor.
I'm not sure why a constructor would want to use delete, so sure, it's not necessary.
OK I see
nwp
nwp
The conclusion you should arrive at is that delete is unnecessarily complicated and RAII is superior. @Rick
Oh, RAII, the "Goes out of scope" stuff.
One more question.
    class HasPtr {
    public:
           HasPtr(const std::string &s = std::string()) :
                  ps(new std::string(s)), i(0) {
                  std::cout << *ps << std::endl;
           }

    private:
           std::string *ps;
           int i;
    };
if I initialize a local instance of HasPtr, when it goes out of scope, does it cause a memory leak?
nwp
nwp
You used new without delete, that usually means yes.
HasPtr will correctly clean up ps and i, but nobody cleans up *ps.
Ok
Then what about this:
What if I use new to dynamically allocate a HasPtr instance? When I use delete, does it also free dynamic allocated memory pointed by ps?
{HasPtr * p1 = new HasPtr(); delete p1;}
nwp
nwp
07:26
No. It deletes p1 which calls p1's destructor which fails to delete ps;.
Wow, that's out of my expectation. Let me think about it..
So for this kind of class (situation), the programmer should implement the destructor to delete ps?
class HasPtr {
public:
	HasPtr(const std::string &s = std::string()) :
		ps(new std::string(s)), i(0) {
		std::cout << *ps << std::endl;
	}

	~HasPtr(){
		delete ps;
	}

private:
	std::string *ps;
	int i;
};
nwp
nwp
The programmer should simply use std::string ps; and everything works fine. In case that isn't possible for some reason you use std::unique_ptr<std::string> ps; which automatically deletes the string when destructed.
@Rick That is a bit better but you didn't obey the rule of 0/5 so it still leaks, for example when you copy a HasPtr.
Haha sure yes. Sorry I am thinking a little bit too much and out of practical usage.
nwp
nwp
It's good to get this cleared up. While complicated at the beginning it does eventually make things much easier.
Ya. I just wanted to be clear about this stuff in language level for the example that I asked. After clearing these, certainly I would choose the best way to do it.
Thank you very much ;D. I will write it down in my notebook
 
3 hours later…
nwp
nwp
11:08
@nwp It doesn't leak, it does a double-delete.
?
What
nwp
nwp
When you copy a HasPtr it copies the ps. Then one HasPtr goes out of scope and does delete ps; which leaves the other HasPtr with an invalid pointer. Then it goes out of scope and your program probably crashes when trying to delete an invalid pointer.
Oh. you are talking about that code which needs a copy constructor
Ya. I would implement that in practice. ;D. The rule of 3/5
I am not very familiar with chatroom usage. I thought you were talking about something else. I saw I curved arrow from your last message :D.
11:41
why do you need the string* anyway?
It's a exercise example from the C++ Primer...
I am not coming up this kind of idea from nowhere. :D
And also I think the answer from the solution book is not right. There are some flaws of this book.
class HasPtr {
public:
	HasPtr(const std::string &s = std::string()) :
		ps(new std::string(s)), i(0) {
		std::cout << *ps << std::endl;
	}

	HasPtr(const HasPtr &hp){
		ps = new string(*hp.ps);   // This causes memory leak I think
		i = hp.i;  //After copying, there's no way to delete the memory pointed by the left side original ps.
	}

private:
	std::string *ps;
	int i;
};
12:10
the copy constructor is called when the dst object is not initialized and when the src object will still be alive after
so this.ps is a garbage value and i is just a value
Oh yes!
Stupid me I ignored that
Thank youuuu for telling me !

« first day (733 days earlier)      last day (2344 days later) »