Writing S-Functions    

Memory Allocation

When you are creating an S-function, the available work vectors might not provide enough capability. In this case, you need to allocate memory for each instance of your S-function. The standard MATLAB API memory allocation routines mxCalloc and mxFree should not be used with C MEX S-functions, because 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 library routines calloc and free. 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.


  Example Involving a Pointer Work Vector Function-Call Subsystems