5 ms·
No garbage collection. But, modern C++ recommendations pretty much deprecate manual memory management for 90% of situations. std::vector and std::make_unique
by corysama 10y ago
No garbage collection. But, modern C++ recommendations pretty much deprecate manual memory management for 90% of situations. std::vector and std::make_unique for 80% of everything; other containers and make_shared when you really have to; malloc and new for specialty situations.
- gowld 10y agoThat's still a lot more work and error-prone than using garbage-collection.
- sqeaky 10y agoit more like 99% of the time and as long you don't go near new or malloc you generally can't leak. Then with RAII you can clean up any kind of resource. Most languages have pretty rudimentary tools for closing files and worse for releasing mutexes. These tasks are almost easy to get right in modern C++.
- WildUtah 10y agoI still love 'with-open-file and its sisters in LISP. Get that resource in one line and it's guaranteed to be cleaned up right without ever thinking about it again. Then you have total freedom to use it however you want and the results destructure themselves.
- civility 10y agoYou could implement something comparable: int main() { with_open_file_for_writing("foo.dat", [](FILE* fp) { fprintf(fp, "old style I/O, automatically closed\n"); }); return 0; }
- Animats 10y ago"With" is even better in Python. Exceptions during closing work right. The big problem with RAII is that if anything goes wrong in a destructor, you're in trouble.
- Cyph0n 10y agoNewbie C++ programmer here. Firstly, what exactly is the difference between make_unique and plain old unique_ptr? Why do I need to include a separate template to use the former? A related issue I faced today. Say I want to create a std::vector of objects. From what I understand, I have three options. 1) Use raw pointers, but be sure to cleanup using delete. 2) Use shared_ptr, which will ensure that your object is safely deleted once no one is referencing it. 3) Create a stack allocated object, then push that onto the vector. The vector will create a shallow copy of the object, and the original copy will be deleted at the end of the current scope. In this scenario, I wouldn't be able to use unique_ptr, because that would mean that both the vector and the local scope have pointers to that object. Am I correct? Are there other options for achieving this that I don't know of? Also, which one is the best approach for the general case? I ended up using shared_ptr and it seems to be working fine... I think.
- hellofunk 10y ago>what exactly is the difference between make_unique and plain old unique_ptr make_unique is just a convenience function for creating a unique_ptr without having to write "new" yourself. It returns a unique_ptr.
- gcp 10y agoYou want std::vector::emplace_back. I think technically you could use unique_ptr and transfer ownership (move) when pushing into the vector, but the underlying "bad pattern" here is that you almost never want to have a vector of (smart) pointers. The vector should own the objects directly.
- hellofunk 10y ago> Say I want to create a std::vector of objects. From what I understand, I have three options. Vectors already own their contents, so there is no need to use unique_ptr here. The objects will be allocated on the heap either way, and you don't need a stack object either. Just push or emplace the object into your vector and be done with it. When the vector is deleted, so will its contents, automatically.
- 10y ago