2010-10-22 185 views
0

在Visual C++中,我試圖動態分配一些16字節對齊的內存,所以我可以使用需要內存對齊的SSE2函數。現在,我這是怎麼分配的內存:boost :: shared_array和對齊的內存分配

boost::shared_array aData(new unsigned char[GetSomeSizeToAllocate()]);

我知道我可以使用_aligned_malloc分配對齊的存儲空間,但會引起與提升一個問題,當它試圖釋放我的記憶?這是代碼升壓用來釋放內存:

 
template inline void checked_array_delete(T * x) 
{ 
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ]; 
    (void) sizeof(type_must_be_complete); 
    delete [] x; 
} 

Memory free'd by delete must be allocated with new, right? Any tip on how I can get around this?

回答

1

boost::shared_array有一個構造函數有刪除的第二個參數將被用來取代默認的delete[]

這意味着您可能可以像這樣傳遞合適的釋放函數的地址。

boost::shared_array<X> array(allocate_x(100), &deallocate_x); 

參考文獻:Boost.SharedArray