简体   繁体   English

类私有成员C ++的动态内存分配与std :: vector

[英]Dynamic memory allocation vs std::vector for class private member C++

While learning c++ I usually (very often) encounter the following advice: "Avoid dynamic memory allocation as much as you can ; use std::vectors instead as they handle this for you". 虽然学习C ++我一般(经常)会遇到以下忠告:“不要动态内存分配尽可能多的,你可以 ;使用std ::向量,而不是因为他们处理这个问题你。”

So my question is: When do I have to use dynamic memory allocation? 所以我的问题是:什么时候必须使用动态内存分配? All the exercises I have done (I am just a beginner) are much easier using std::vector ; 使用std::vector可以轻松完成所有的练习(我只是一个初学者); nevertheless my lecturer forces us to use dynamic memory for simple classes (like matrices, geometric vectors, etc.) and delete[] in the destructor. 但是,我的讲师迫使我们对简单的类(例如矩阵,几何矢量等)使用动态内存,并在析构函数中使用delete[]

The only advantage I have found so far for new;delete[]; 到目前为止,我发现的唯一优点是new;delete[]; (or at least what I tell my self in order to feel that it is worth using dynamic memory) is using move copy and move assignment. (或者至少我告诉我的自我,以便觉得使用动态内存值得)是使用移动复制和移动分配。

std::vector also does dynamic memory allocation behind the scenes (by using the new operator). std::vector也可以在后台进行动态内存分配(通过使用new运算符)。 Copy and move assignment are also defined for std::vector , as you can see here , so there is no speed gained if you do it by hand. 如您在此处看到的,还为std::vector定义了复制和移动分配,因此,如果手动进行操作,则不会获得任何速度。

Probably your question refers to when should you manually allocate memory (by explicitly using new and delete ), as opposed to relying on another class (such as vector) to do it for you. 可能您的问题是指您何时应该手动分配内存(通过显式使用newdelete ),而不是依赖于另一个类(例如vector)来为您完成此任务。

The "Modern C++" answer to this question is to never do memory management by hand. 这个问题的“现代C ++”答案是永远不要手工进行内存管理。 If an std::vector does the job, then use that instead. 如果std::vector完成了任务,请改用它。 If you need to allocate a single item, then use std::unique_ptr . 如果需要分配单个项目,请使用std::unique_ptr

There are situations where you may need to implement a custom container (because neither std::vector nor any of the other standard library containers fit your purpose), and in those cases it may make sense to do the memory management manually. 在某些情况下,您可能需要实现自定义容器(因为std::vector和其他任何标准库容器都不符合您的目的),在这种情况下,手动进行内存管理可能很有意义。

However, unless you are providing the low-level core components in some industrial grade code base and have very specific goals, doing manual memory management is probably still not necessary here either. 但是,除非您在某些工业级代码库中提供低级核心组件并且有非常特定的目标,否则在这里也仍然没有必要进行手动内存管理。

I agree with the strict rules of your lecturer insofar that it pays to understand what happens behind the scenes. 我同意您的讲师的严格规定,以了解幕后发生的事情是值得的。 You rarely, if ever, should have to use manual memory management, but it helps to understand how std::unique_ptr / std::vector ( have to ) do it for you in order to understand why C++ was built the way it is. 你很少,如果有的话, 应该使用手动内存管理,但它有助于了解std::unique_ptr / std::vector )为你做它为了理解为什么C ++建成现在这个样子。 Teaching C++ in a way that is useful in the real world would eventually allow/force you to use standard library containers though, because there is (as you correctly noticed) essentially no reason to ever write a delete . 以一种在现实世界中有用的方式来教授C ++最终将允许/强迫您使用标准库容器,因为(如您所正确注意到的那样)基本上没有理由编写delete

The lecturer most likely forces you so you get an idea of how things work under the hood. 讲师很可能会逼迫您,因此您可以了解幕后工作原理。 The std::vector class as you mentioned handles all the dynamic memory management for you, such that you do not need to write all the code to handle this yourself. 如前所述,std :: vector类可以为您处理所有动态内存管理,因此您无需编写所有代码即可自行处理。 It is easy, especially as a beginner to mess up while handling dynamic memory and create a leak/forget to deallocate something. 这很容易,尤其是对于初学者来说,在处理动态内存时会搞砸并且创建泄漏/忘记分配某些东西。

A vector is however not the solution for every problem, and you may find cases where it does not fit all your critera - and in those cases it could be better to implement your own data structure. 然而,向量并不是解决每个问题的解决方案,您可能会发现它并不适合您的所有标准,在这种情况下,实现自己的数据结构可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM