C++ API and allocators (memory management)
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (1)
Shreeya
il 25 Ago 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Write C++ Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!