2015-05-30 39 views
0

我想在c中使用的物體(「II」,在下面的程序)後釋放存儲器++:免存儲器在C + +(犰狳)

#include <iostream> 
#include <armadillo> 

using namespace std; 
using namespace arma; 

int main() 
{ 
    cx_mat ii(2,2,fill::eye); 
    cout << ii <<endl; 
    free (ii); 
    return 0; 
} 

但是編譯後,我會遇到下面的錯誤:

error: cannot convert ‘arma::cx_mat {aka arma::Mat<std::complex<double> >}’ to ‘void*’ for argument ‘1’ to ‘void free(void*)’ 

可以任何身體指導我嗎?

+3

'ii'具有「自動存儲持續時間」,所以它的內存被釋放在函數返回時。如果您的書或教程建議您手動完成,請換一本新書。如果你沒有書,拿一本。 – molbdnilo

+0

你似乎缺乏一些非常基礎的知識。你應該閱讀[書](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 –

回答

1

可以free指針持有地址由malloccallocrealloc返回。沒有其他的。特別是,沒有矩陣。

您也不需要在C++中使用free

就你而言,你不需要做任何事情來釋放內存,矩陣的析構函數會照顧到這一點。

1

你得到錯誤的原因是因爲你沒有傳遞函數指針。

然而,free requires您傳遞已由malloccalloc,或者realloc返回一個指針,否則會發生未定義的行爲。

當函數返回或退出時,對象的內存將被釋放。