[英]memory allocation and deallocation of struct c++
struct SAD_tables
{
typedef int** sadTable;
sadTable sadTables[16];
int height;
int width;
SAD_tables(int _height, int _width)
{
height = _height;
width = _width;
for (int i = 0; i < 16; i++)
{
sadTables[i] = new int*[height];
for (int j = 0; j < height; j++)
{
sadTables[i][j] = new int[width];
}
}
}
~SAD_tables()
{
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < height; j++)
{
delete sadTables[i][j];
}
delete[] sadTables[i];
}
}
};
我不確定我是否正確實現了刪除功能。 你可以解釋我是否正在使用刪除[]並正確刪除?
如果您堅持手動處理內存而不是使用正確的容器類,則需要修復new / delete。 你擁有的每個new
調用都是數組形式的new ...[]
所以每個delete
應該是delete[]
,但你在循環中使用delete
而不是[]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.