matlab coder: It's time to generate a modern C++ API

8 views (last 30 days)
The matlab coder product is very widely used in industry, and since the R2011 version, the generation is still mainly oriented towards the C API interface (C++ is actually a layer of wrapper for C) to common one-/two-dimensional arrays, colum-major, involving low-level operations such as memory management allocation and release. As history progressed, we moved more towards the modern form of the C++ API (C++11), with safer use of smart pointers, threaded support, and better security and convenience similar to the mex C++ API introduced by MATLAB R2017b. Using the older C API can often cause memory management problems and be difficult to debug.
Once we have implemented our algorithms quickly in matlab, we often have to iterate matlab syntax often on generating C/C++ code, and array bounds often have to be considered, as it is particularly important in C to consider is allocating memory on the stack and heap. The array size in the current generated C code can easily be very large, e.g. myarray[largeNumber] in image processing toolbox, and then a Segmentation Fault error will occur. This adds a lot of annoying overhead and requires a lot of effort to keep changing the code. It would be much better if the generated C++ code was in the new C++11, such as std::vector, which does not take into account the array size limit and allows the developer to focus on the algorithm itself.

Accepted Answer

Matan Silver
Matan Silver on 2 Nov 2022
Edited: Walter Roberson on 4 Nov 2022
Thank you for the feedback. We agree! There's a lot of opportunity to take advantage of more modern C++ features. I've passed on these notes to the MATLAB Coder development team, and we'll consider them going forward.
A couple things which could make a difference, if you are not aware:
MATLAB Coder doesn't generate code that calls native C++ threading APIs, but we do emit calls to openMP in certain cases. See:
For smart pointers, while MATLAB Coder doesn't generate shared or unique pointers, it can wrap dynamic memory allocation in a more modern interface that guards calls to "new" with a wrapper called coder::array. This should be safer than emxArrays. You can do this by setting DynamicMemoryAllocationInterface to 'C++' which should be the default when generating C++ code. See:
You mention large stack allocations--this can certaintly be a problem, especially in constrained systems. There is one configuration parameter, DynamicMemoryAllocationThreshold, which can be used to decrease the maximum size variable that can be allocated on the stack. This comes at the cost of more uses of heap allocation:
I'll pass along your notes about these use-cases, which I have understood to be:
- Using smart pointers for dynamic memory allocation and tracking of pointer ownership
- Using std::vector to store numerical data
- Generating MEX using the C++ MEX API instead of the C MEX API
- Using dynamic memory allocation even for fixed size arrays that are currently allocated on the stack
Let me know if I've missed anything, and thank you again,

Sign in to comment.

More Answers (0)


Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!