2017-06-19 71 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