MATLAB: Is it possible to use mxCalloc and mxFree in S-Functions

crashsimulink

I created identical S-Functions with dynamic memory allocation. One uses calloc/free and it works fine. The other one uses mxCalloc/mxFree and it crashes every now and then when simulating.

Best Answer

Standard MATLAB API memory allocation routines mxCalloc and mxFree are designed to be used with MEX-files that are called from MATLAB, not from Simulink. In S-Functions, use the stdlib.h library routines calloc and free.