Writing S-Functions    

Memory Allocation

When creating an S-function, it is possible that the available work vectors don't provide enough capability. In this case, you will need to allocate memory for each instance of your S-function. The standard MATLAB API memory allocation routines (mxCalloc, mxFree) should not be used with C MEX S-functions. The reason is that these routines are designed to be used with MEX-files that are called from MATLAB and not Simulink. The correct approach for allocating memory is to use the stdlib.h (calloc, free) library routines. In mdlStart allocate and initialize the memory and place the pointer to it either in pointer-work vector elements

or attach it as user data.

In mdlTerminate, free the allocated memory.


 An Example Involving a Pointer Work Vector Function-Call Subsystems