簡體   English   中英

c ++指針的內存分配向量

[英]c++ memory allocation vector of pointers

我正在並行化代碼,因此使用結構parallel_vector(ppl)。 問題與標准的std :: vector相同......

我首先構建一個指向結構的指針向量。 結構是巨大的,原始類型成員與巨大的原始類型數組(23000個元素)一起。

我為這個結構實現了一個深拷貝拷貝構造函數。

然后,我循環訪問此列表中的元素。

for (int ii=0; ii < nbBlocks; ii++)
{
    MyStruct* Block_temp = list_structs.at( ii );
    // ...
}

當我訪問位置ii的元素時,我是否創建了一個具有內存分配的新對象? 我應該刪除當前循環結束時的Block_temp,還是會破壞向量中包含的對象?

謝謝

你有一個指向已經在向量中的內容的指針,向量似乎擁有你的數據,所以不要刪除它。 復制指針不是分配。

考慮:

int* a = new int;
int* b = a; // Pretty much what you are doing
delete a; // If you deleted b, then this would be a double delete, and using a or b after that point would be bad

請參閱std :: vector :: at的文檔

http://www.cplusplus.com/reference/vector/vector/at/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM