我得到了一個循環分配的一些內存 - 如何釋放它,當我與tr_data變量做了什麼? (我是相當新的C++)免費存儲
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
struct svm_problem tr_data;
tr_data.l = (int) prm_num_samples_anchored.array[bar];
tr_data.y = Malloc(double, tr_data.l);
tr_data.x = Malloc(struct svm_node*, tr_data.l);
for (int row = 0; row < tr_data.l; row++)
{
tr_data.y[row] = ta0.array[bar-row-1];
//leak
svm_node* tr_data_x_onerow = Malloc(svm_node, num_features+1);
tr_data_x_onerow[0].index = 1; tr_data_x_onerow[0].value = in0.array[bar-row-1]; tr_data_x_onerow[1].index = 2; tr_data_x_onerow[1].value = in1.array[bar-row-1]; tr_data_x_onerow[2].index = 3; tr_data_x_onerow[2].value = in2.array[bar-row-1]; tr_data_x_onerow[3].index = 4; tr_data_x_onerow[3].value = in3.array[bar-row-1]; tr_data_x_onerow[4].index = 5; tr_data_x_onerow[4].value = in4.array[bar-row-1]; tr_data_x_onerow[5].index = 6; tr_data_x_onerow[5].value = in5.array[bar-row-1]; tr_data_x_onerow[6].index = 7; tr_data_x_onerow[6].value = in6.array[bar-row-1]; tr_data_x_onerow[7].index = 8; tr_data_x_onerow[7].value = in7.array[bar-row-1]; tr_data_x_onerow[8].index = 9; tr_data_x_onerow[8].value = in8.array[bar-row-1]; tr_data_x_onerow[9].index = 10;
tr_data_x_onerow[num_features].index = -1; //Each row of properties should be terminated with a -1 according to the readme
tr_data.x[row] = tr_data_x_onerow;
}
...上tr_data 幾個操作...這不起作用
for (int row = 0; row <tr_data.l; row++)
{
free(tr_data_x_onerow);
}
你可能需要'自由(tr_data.x [行]);'在你的第二個循環,因爲'svm_node * tr_data_x_onerow'僅僅是第一循環中可用。 –
一點題外話,你不應該使用malloc /免費在C++ (喜歡共享指針,否則新/刪除) – Zermingore
@Zermingore新/刪除只是漸漸地好了(應該是新的[] /刪除[])。在C++中,你不應該動態地分配數組,並且不需要它。只要使用'的std :: VECTOR' – Jens