Filter löschen
Filter löschen

C++ API and allocators (memory management)

2 Ansichten (letzte 30 Tage)
Marie
Marie am 11 Jul. 2023
Beantwortet: Shreeya am 25 Aug. 2023
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

Antworten (1)

Shreeya
Shreeya am 25 Aug. 2023
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.

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by