開發平台(Platform): (Ex: Win10, Linux, ...)
Win7
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
VC++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
std::vector
問題(Question):
assign value完之後 最後如何delete
餵入的資料(Input):
會有5個structure A
裡面有一個float 以及另一結構4份資料
預期的正確結果(Expected Output):
資料memory都清空
錯誤結果(Wrong Output):
資料沒有全放掉
程式碼(Code):(請善用置底文網頁, 記得排版)
struct point
{
int X;
int Y;
};
struct A
{
float fSpeed;
std::vector<point*>pointVec;
};
// Assign value
A* pA = new A[5];
for(int i = 0 ; i < 5 ; i++)
{
pA[i]->fSpeed = 2.0;
for(int nPointIndex = 0 ; nPointIndex < 4 ; nPointIndex++)
{
point* pPoint = new point();
pPoint.X = ...
pPoint.Y = ...
pA[i]->pointVec.push_back(pPoint);
}
}
// Delete
Method 1
if(pA)
{
delete [] pA;
pA = NULL;
}
Method 2
for(int i = 0 ; i < 5 ; i++)
{
auto itr = pA[i]->pointVec.begin();
for(; itr != pA[i]->pointVec.end(); itr++)
{
delete &itr;
}
}
補充說明(Supplement):
以上兩種方法會都清空嗎?
還是有沒有更好的delete方式
謝謝