When do I have to free memory?

c++, memory

Solution

You basically got it right: You need to balance `new` with `delete`, `new[]` with `delete[]`, and `malloc` with `free`.

Well-written C++ will contain almost none of those, since you leave the responsibiltiy for dynamic memo­ry and lifetime management to suitable container or manager classes, most notably `std::vector` and `std::unique_ptr`.

Problem

I learned C# and now I'm learning C++. The whole point of releasing a memory is new for me, and I want to know when I need to worry about memory releasing and when I don't. From what I understand, the only case I have to worry about the release of memory, is when I used `new` operator, so I should to release the memory by using `delete`. But in these cases there is no need to release the memory: - Class variables (Members), or static variables. - Local variables in function. - STL family (string, list, vector, etc.). Is this true? And are there other cases where I have to worry about memory releasing?

Original source