C++ API and allocators (memory management)

2 次查看(过去 30 天)
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