| Using the C Math Library | ![]() |
Replacing Allocation and Deallocation Routines
The MATLAB C Math Library calls mxMalloc to allocate memory and mxFree to free memory. These routines in turn call the standard C runtime library routines malloc and free.
If your application requires a different memory management implementation, you can register your allocation and deallocation routines with the MATLAB C Math Library by calling the function mlfSetLibraryAllocFcns().
void mlfSetLibraryAllocFcns(calloc_proc calloc_fcn, free_proc free_fcn, realloc_proc realloc_fcn, malloc_proc malloc_fcn);
You must write four functions whose addresses you then pass to mlfSetLibraryAllocFcns():
calloc_fcn is the name of the function that mxCalloc uses to perform memory allocation operations. The function that you write must have the prototype:void * callocfcn(size_t nmemb, size_t size);
Your function should initialize the memory it allocates to 0 and should
return NULL for requests of size 0.
free_fcn is the name of the function that mxFree uses to perform memory deallocation (freeing) operations. The function that you write must have the prototype:void freefcn(void *ptr);
Make sure your function handles NULL pointers. free_fcn(0) should do
nothing.
mxRealloc uses to perform memory reallocation operations. The function that you write must have the prototype:void * reallocfcn(void *ptr, size_t size);
This function must grow or shrink memory. It returns a pointer to the requested amount of memory, which contains as much as possible of the previous contents.
malloc to perform memory allocation operations. The prototype for your function must match:void * mallocfcn(size_t n);
Your function should return NULL for requests of size 0.
Refer to the MATLAB Application Program Interface Reference online help for more detailed information about writing these functions.
| Restrictions | Indexing into Arrays | ![]() |