Garbage Collection in C++ — why?

后端 未结 16 1980
北恋
北恋 2020-11-29 18:32

I keep hearing people complaining that C++ doesn\'t have garbage collection. I also hear that the C++ Standards Committee is looking at adding it to the language. I\'m afrai

相关标签:
16条回答
  • 2020-11-29 18:47

    The motivating factor for GC support in C++ appears to be lambda programming, anonymous functions etc. It turns out that lambda libraries benefit from the ability to allocate memory without caring about cleanup. The benefit for ordinary developers would be simpler, more reliable and faster compiling lambda libraries.

    GC also helps simulate infinite memory; the only reason you need to delete PODs is that you need to recycle memory. If you have either GC or infinite memory, there is no need to delete PODs anymore.

    0 讨论(0)
  • 2020-11-29 18:47

    The committee isn't adding garbage-collection, they are adding a couple of features that allow garbage collection to be more safely implemented. Only time will tell whether they actually have any effect whatsoever on future compilers. The specific implementations could vary widely, but will most likely involve reachability-based collection, which could involve a slight hang, depending on how it's done.

    One thing is, though, no standards-conformant garbage collector will be able to call destructors - only to silently reuse lost memory.

    0 讨论(0)
  • 2020-11-29 18:47

    I, too, have doubts that C++ commitee is adding a full-fledged garbage collection to the standard.

    But I would say that the main reason for adding/having garbage collection in modern language is that there are too few good reasons against garbage collection. Since eighties there were several huge advances in the field of memory management and garbage collection and I believe there are even garbage collection strategies that could give you soft-real-time-like guarantees (like, "GC won't take more than .... in the worst case").

    0 讨论(0)
  • 2020-11-29 18:48

    It's an all-to-common error to assume that because C++ does not have garbage collection baked into the language, you can't use garbage collection in C++ period. This is nonsense. I know of elite C++ programmers who use the Boehm collector as a matter of course in their work.

    0 讨论(0)
  • 2020-11-29 18:48

    Garbage collection makes RCU lockless synchronization much easier to implement correctly and efficiently.

    0 讨论(0)
  • 2020-11-29 18:54

    What advantages could garbage collection offer an experienced C++ developer?

    Not having to chase down resource leaks in your less-experienced colleagues' code.

    0 讨论(0)
提交回复
热议问题