필터 지우기
필터 지우기

C++ API and allocators (memory management)

조회 수: 2 (최근 30일)
Marie
Marie 2023년 7월 11일
답변: Shreeya 2023년 8월 25일
I have a mex function that I am updating to use the C++ API so that I can create string arrays as opposed to cell arrays of characters. I use standard library functions (e.g. std::regex_iterator) that rely on memory allocation routines within the call. When I used the C API, I had a custom allocator template class (mex_allocator<T>) that let the standard libraries use Matlab's memory allocation (e.g., MxMalloc), similar in vain to @hokiedsp's allocator class used in matlab-mexutils. I then bound the standard library functions such that the custom allocator class was used, e.g.:
// Use custom Matlab allocator in these STL functions
namespace mxalloc {
using string = std::basic_string<char, std::char_traits<char>,
mex_allocator<char>>;
using smatch =
std::match_results<mxalloc::string::const_iterator,
mex_allocator<std::string::const_iterator>>;
... other routines
}
The C++ API does not list mxMalloc and mxFree as functions and indicates that the C and C++ interfaces should not be mixed. Are there an alternative memory allocation routines that I can use in a similar manner? In the past (circa R2017b?), I have had issues with letting the STL use the native memory allocation.
Thanks - Marie

답변 (1개)

Shreeya
Shreeya 2023년 8월 25일
Hello Marie
I understand you want to create string arrays as opposed to cell arrays of characters which requires a custom memory allocator template for C++, similar to C.
Find the wrapper around mxMalloc and mxFree API’s for C++ in the file exchange below for an alternate memory allocation routine.
Hope this helps.

카테고리

Help CenterFile Exchange에서 Write C++ Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by