Using the C Math Library | ![]() ![]() |
Allocating and Freeing MATLAB Arrays
When you create a MATLAB array, using any of the array creation mechanisms, the MATLAB C Math Library allocates the storage for the array. The responsibility for freeing the allocated storage is shared between you and the library automated memory management facility.
When automated memory management is enabled, all the arrays returned by library routines are temporary. That is, when these arrays are passed to another library routine, that routine destroys the array before returning. This capability allows you to nest, or compose, calls to MATLAB C Math Library routines without causing memory leaks. The calls to the mlfScalar()
routine which are nested in the examples in this chapter illustrate routine nesting.
If your application needs to use an array several times, you must assign the array to an mxArray
pointer variable to make it persist. This is called binding the array to a variable. You use the mlfAssign()
routine to bind an array to an array pointer variable. Arrays returned as output arguments are bound to variables automatically by the library. Any array you bind to a variable you must explicitly free. Use the mxDestroyArray()
routine to free bound arrays.
All the code examples in this chapter assume that the MATLAB C Math Library automated memory management is enabled. For information about enabling memory management, see Chapter 4.
![]() | Performing Common Array Programming Tasks | Displaying MATLAB Arrays | ![]() |