是否有任何有效的用例在现代C ++中使用new和delete,raw指针或c样式数组? [英] Are there any valid use cases to use new and delete, raw pointers or c-style arrays with modern C++?

查看:72
本文介绍了是否有任何有效的用例在现代C ++中使用new和delete,raw指针或c样式数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这是一个值得注意的视频(停止教C 关于在教授c ++语言时需要进行的范式更改。

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


我有一个梦想...

I have a dream ...

我在梦想所谓的C ++课程/班级/课程将停止教(要求)学生使用:...

I'm dreaming of so called C++ courses/classes/curriculae will stop teaching (requiring) their students to use: ...

自从C ++ 11作为既定标准以来,我们就有了 动态内存管理 设施也称为智能指针

即使从较早的标准中也可以使用c ++标准 容器库 很好的替代原始数组(分配给 new T [] )(尤其是 std :: string 而不是c风格的 NUL 终止的字符数组)。

Since C++11 as established standard we have the Dynamic memory management facilities aka smart pointers.
Even from earlier standards we have the c++ standard Containers library as a good replacement for raw arrays (allocated with new T[]) (notably usage of std::string instead of c-style NUL terminated character arrays).

粗体中的问题:

放置 new 替代,是否存在任何使用智能指针或标准容器无法实现的有效用例,而只能使用 new 直接删除 (当然除了实现此类容器/智能指针类外)?

Let aside the placement new override, is there any valid use case that can't be achieved using smart pointers or standard containers but only using new and delete directly (besides implementation of such container/smart pointer classes of course)?

有时会传闻他们(例如此处此处)使用 new del在某些情况下,手动滚动操作可能会更有效。 这些实际上是什么?这些极端情况是否不需要以与标准容器或智能指针相同的方式来跟踪分配?

It's sometimes rumored (like here or here) that using new and delete handrolled can be "more efficient" for certain cases. Which are these actually? Don't these edge cases need to keep track of the allocations the same way as standard containers or smart pointers need to do?

几乎原始c样式固定大小的数组也是如此:如今有 std :: array ,它允许各种赋值,复制,引用等,它们在语法上容易且一致每个人都期望的。 有没有用例选择 T myArray [N]; c样式的数组,而不是选择 std :: array< T, N> myArray;

Almost the same for raw c-style fixed size arrays: There is std::array nowadays, which allows all kinds of assignment, copying, referencing, etc. easily and syntactically consistent as expected by everyone. Are there any use cases to choose a T myArray[N]; c-style array in preference of std::array<T,N> myArray;?

关于与第三方库的交互:

Regarding interaction with 3rd party libraries:

假定第3方库返回分配有 new 的原始指针,例如

Assumed a 3rd party library returns raw pointers allocated with new like

MyType* LibApi::CreateNewType() {
    return new MyType(someParams);
}

您始终可以将其包装到智能指针中,以确保删除称为:

you can always wrap that to a smart pointer to ensure that delete is called:

std::unique_ptr<MyType> foo = LibApi::CreateNewType();

即使API要求您调用其旧版功能以释放资源,例如

even if the API requires you to call their legacy function to free the resource like

void LibApi::FreeMyType(MyType* foo);

您仍然可以提供删除功能:

you still can provide a deleter function:

std::unique_ptr<MyType, LibApi::FreeMyType> foo = LibApi::CreateNewType();




我对有效的每天特别感兴趣 em>用例与学术/教育目的要求和限制相反,这些要求和限制未包括在上述标准设施中。

new delete 可能用于内存管理/垃圾收集器框架,或者标准容器实现毫无疑问 1


I'm especially interested in valid "every day" use cases in contrast to academic/educational purpose requirements and restrictions, which aren't covered by the mentioned standard facilities.
That new and delete may be used in memory management / garbage collector frameworks or standard container implementation is out of question1.

...问这个问题是为了给其他人一个替代方法(作业)问题,这些问题只能使用标题中提到的任何构造,但对于生产就绪代码则是严重的问题。

... to ask this question is to give an alternative approach vs any (homework) questions, which are restricted to use any of the constructs mentioned in the title, but serious questions about production ready code.

这些通常称为基本 em>内存管理,这是IMO公然错误/被误解为适合初学者的讲座和任务。

These are often referred to as the basics of memory management, which is IMO blatantly wrong/misunderstood as suitable for beginners lectures and tasks.

1) 添加。:关于该段,这应该清楚地表明 new delete 不适合初学者c ++学生,但应该留给

1)Add.: Regarding that paragraph, this should be a clear indicator that new and delete isn't for beginner c++ students, but should be left for the more advanced courses.

推荐答案

所有权不应该是本地的。

When ownership should not be local.

作为一个示例,指针容器可能不希望对指针的所有权驻留在指针本身中。如果您尝试编写带有向前唯一ptrs的链表,那么在销毁时,您可以轻松地破坏堆栈。

As an example, a pointer container may not want ownership over the pointers in it to reside in the pointers themselves. If you try to write a linked list with forward unique ptrs, at destruction time you can easily blow the stack.

A vector 的拥有指针的容器可能更适合在容器或子容器级别而不是元素级别存储删除操作。

A vector-like container of owning pointers may be better suited to storing delete operation at the container or subcontainer level, and not at the element level.

在这种情况下,您可以像智能指针一样包装所有权,但是可以在更高层次上进行。许多数据结构(图形等)可能也存在类似的问题,即所有权比指针正确地位于更高的位置,并且它们可能不直接映射到现有的容器概念。

In those and similar cases, you wrap ownership like a smart pointer does, but you do it at a higher level. Many data structures (graphs, etc) may have similar issues, where ownership properly resides at a higher point than where the pointers are, and they may not map directly to an existing container concept.

在某些情况下,可能很容易从其余数据结构中排除容器所有权。在某些情况下可能并非如此。

In some cases it may be easy to factor out the container-ownership from the rest of the data structure. In others it may not.

有时您会疯狂地计算非本地非参考计数的生命周期。在这些情况下,没有指向所有权指针的理智的地方。

Sometimes you have insanely complex non-local non-reference counted lifetimes. There is no sane spot to put the ownership pointer in those cases.

在这里确定正确性很困难,但并非不可能。存在正确且具有这种复杂所有权语义的程序。

Determining correctness here is hard, but not impossible. Programs that are correct and have such complex ownership semantics exist.

所有这些都是极端情况,很少有程序员应该运行在他们的职业生涯中经历了很多次。

All of these are corner cases, and few programmers should run into them more than a handful of times in a career.

这篇关于是否有任何有效的用例在现代C ++中使用new和delete,raw指针或c样式数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆