4 ms·
Doesnt std::shared_ptr use garbage collection?
by orangepanda 3y ago
Doesnt std::shared_ptr use garbage collection?
- zaphoyd 3y agostd::shared_ptr is reference counted. No GC involved.
- cpitman 3y agoNo, it uses reference counting, internal to the shared pointer implementation itself.
- conradludgate 3y agoIt uses reference counting and RAII. It does not use reachability analysis or tracing. Many people refer to a tracing routine or reachability analysis when they refer to garbage collectors
- repelsteeltje 3y agoNo, std::shared_ptr is plain ref counting
- eatonphil 3y agoIf you subscribe to the unified theory of garbage collection [0] then yes shared_ptr does use garbage collection. See also Herb Sutter [1] (describing a different project, but also mentioning shared_ptr): > Q: "Is this garbage collection?" > Of course, and remember that so is reference counting (e.g., shared_ptr). [0] https://www.cs.cornell.edu/courses/cs6120/2019fa/blog/unified-theory-gc/ https://www.cs.cornell.edu/courses/cs6120/2019fa/blog/unifie... [1] https://github.com/hsutter/gcpp#q-is-this-garbage-collection https://github.com/hsutter/gcpp#q-is-this-garbage-collection
- simiones 3y agoIt's a matter of definitions. Technically, reference counting is a form of garbage collection, and it is often discussed in the garbage collection literature. Most commonly though, "garbage collection" refers to global schemes which apply by default to all memory (perhaps with rare exceptions such as pinning). So, in typical usage, C++ smart pointers, being opt-in, are not considered garbage collection. Languages which do automatic reference counting globally, such as Python or Swift, are indeed considered garbage collection schemes.
- Sohcahtoa82 3y ago> Technically, reference counting is a form of garbage collection, and it is often discussed in the garbage collection literature. This is something I always thought was true, but then I see people talk about reference counting VERSUS garbage collection, when I was taught that reference counting is a subset of garbage collection. In other words, "garbage collection" merely refers to any method of automatic memory management that the programmer doesn't have to think about, and reference counting is merely one method of implementing garbage collecting. Heck, Python uses reference counting, yet the library for directly interacting with the reference counter is called "gc", for "garbage collection".
- simiones 3y agoDifferent terms can have (slightly) different meanings in different contexts. As I said, I think in practice any form of memory management that the language runtime/compiler does for you is considered garbage collection in the colloquial sense, but any such system where you have to use a specific language construct (such as std::shared_ptr or Rc) are not. I'd also note that Python doesn't just do reference counting: it also has a tracing garbage collector that collects unreachable reference cycles. I'm not sure if Swift does something similar.
- jcranmer 3y agoI would call a reference-counted mechanism automatic memory management. I would only call it garbage collection if there were a mechanism to try to collect cycles among reference counts.
- ReleaseCandidat 3y agoDepends on your definition of "GC". But used in the "usual" way, it includes reference counting. Wikipedia agrees: https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Reference_counting https://en.wikipedia.org/wiki/Garbage_collection_(computer_s...
- KerrAvon 3y agoThis is a silly argument, though. GC has a clear de facto meaning. If you say "Garbage Collection" in a room full of programmers, they're going to assume the traditional meaning, as seen in Lisp and Java, where you have something capable of automatically collecting cycles without programmer intervention. I've seen people claim malloc and free are GC. And sure, you can get there, but it makes the term utterly meaningless.
- pjmlp 3y agoOnly if they are the kind of those that call themselves "engineer" without a professional Software Engineering degree. Apparently you aren't even aware there are Lisp and Java implementations with reference counting algorithms for garbage collection.
- Dylan16807 3y ago> Apparently you aren't even aware 1. That's a pretty ungenerous interpretation of "as seen in". 2. Are people expected to know about those specific implementations or they're a bad programmer? If that's your intent then screw off. Also that sounds like a broken way to do "Java" without qualifiers. For Lisp, eh, you can even use a bump allocator if you want but that doesn't mean everyone should consider it every time they talk about Lisp.
- pjmlp 3y agoYes, that is exactly my intent, thank you very much.