4 ms·
unique_ptr, the default smart pointer in C++ is no slower than direct pointer access. If you require data sharing, then shared_ptr offers you that feature at th
by bstamour 13y ago
unique_ptr, the default smart pointer in C++ is no slower than direct pointer access. If you require data sharing, then shared_ptr offers you that feature at the cost of reference counting.
- pkolaczk 13y agounique_ptr is not a replacement for Java's GC. It simply doesn't do the same. shared_ptr is much closer, but at the cost of reference counting, which, if you start using it excessively, is much higher than cost of Java GC. I agree that if your memory management is dead simple and you are fine with using only unique_ptr everywhere, C++ solution is superior. You obviously don't need GC, so why pay for it. Probably C would do just as fine, assuming you have some static analysis tool to find all the forgotten free calls. However, there are some domains where GC enables completely different ways of programming. E.g. lockless concurrency / immutable structures / functional programming etc. Poor shared_ptr performance and scalability is prohibitive in those areas.
- pjmlp 13y ago> Probably C would do just as fine, assuming you have some static analysis tool to find all the forgotten free calls. While it helps, it can only cover the code you have access to. No way to take care of third party libraries delivered in binary form.
- pcwalton 13y ago> Probably C would do just as fine, assuming you have some static analysis tool to find all the forgotten free calls. I don't believe such a static analysis tool can exist without being a different language (e.g. Cyclone). There just isn't enough information about ownership semantics in C's type system.
- mcguire 13y agoCough, cough, Rust?[1] [1] http://static.rust-lang.org/doc/0.8/rust.html#memory-boxes http://static.rust-lang.org/doc/0.8/rust.html#memory-boxes
- pkolaczk 13y agoIn cases, where unique_ptr is enough, ownership is simple and statically pairing all mallocs with frees is easy. The whole problem with memory management is sharing ownership. Then things start getting hairy.