Are there any valid use cases to use new and delete, raw pointers or c-style arrays with modern C++?

前端 未结 19 905
梦谈多话
梦谈多话 2020-11-22 07:20

Here\'s a notable video (Stop teaching C) about that paradigm change to take in teaching the c++ language.

And an also notable blog post

相关标签:
19条回答
  • 2020-11-22 08:05

    Another possible valid use case is when you code some garbage collector.

    Imagine that you are coding some Scheme interpreter in C++11 (or some Ocaml bytecode interpreter). That language requires you to code a GC (so you need to code one in C++). So ownership is not local, as answered by Yakk. And you want to garbage collect Scheme values, not raw memory!

    You probably will end up using explicit new and delete.

    In other words, C++11 smart pointers favor some reference counting scheme. But that is a poor GC technique (it is not friendly with circular references, which are common in Scheme).

    For example, a naive way of implementing a simple mark-and-sweep GC would be to collect in some global container all the pointers of Scheme values, etc...

    Read also the GC handbook.

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