2017-06-19 39 views
-7

我想知道如果动态分配内存我使用新的[]或malloc在std :: vector,或不需要,如果我不需要,我应该在哪里使用new [],malloc和smartpointers?C++ stl库容器具有动态内存分配?

+11

听起来像你可以使用[良好的C++书](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver

+0

可能的重复[为什么要C++程序员尽量减少使用'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) –

回答

-4

如果使用C++,最好使用new []方法在堆中分配内存,因为它比malloc方法更安全。

+3

虽然在C++代码中应该避免使用'malloc',如果你使用STL,那么你几乎不需要直接使用'new []'。 – jamesdlin