| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
#include "matrix.h" #include <stdlib.h> void *mxCalloc(mwSize n, mwSize size);
mwPointer mxCalloc(n, size) mwSize n, size
Number of elements to allocate. This must be a nonnegative number.
Number of bytes per element. (The C sizeof operator calculates the number of bytes per element.)
Pointer to the start of the allocated dynamic memory, if successful. If unsuccessful in a stand alone (non-MEX-file) application, mxCalloc returns NULL in C (0 in Fortran). If unsuccessful in a MEX-file, the MEX-file terminates and control returns to the MATLAB prompt.
mxCalloc is unsuccessful when there is insufficient free heap space.
MATLAB applications should always call mxCalloc rather than the ANSI C calloc function to allocate memory. In stand alone applications, such as the MATLAB engine, mxCalloc calls the calloc function. In MEX-files, mxCalloc automatically:
Allocates enough contiguous heap space to hold n elements.
Initializes all n elements to 0.
Registers the returned heap space with the MATLAB memory manager.
How you manage the memory created by this function depends on the purpose of the data assigned to it. If you assign it to an output argument in plhs[] using the mxSetPr function, MATLAB is responsible for freeing the memory.
If you use the data internally, the MATLAB memory manager maintains a list of all memory allocated by the function and automatically frees (deallocates) the memory when control returns to the MATLAB prompt. In general, we recommend that MEX-file functions destroy their own temporary arrays and free their own dynamically allocated memory. It is more efficient to perform this cleanup in the source MEX-file than to rely on the automatic mechanism. Therefore, when you finish using the memory allocated by this function, call mxFree to deallocate the memory.
If you do not assign this data to an output argument, and you want it to persist after the MEX-file completes, call mexMakeMemoryPersistent after calling this function. If you write a MEX-file with persistent memory, be sure to register a mexAtExit function to free allocated memory in the event your MEX-file is cleared.
See
explore.c in the matlabroot/extern/examples/mex folder
phonebook.c and revord.c in the matlabroot/extern/examples/refbook folder
For additional examples, see mxcalcsinglesubscript.c and mxsetdimensions.c in the matlabroot/extern/examples/mx folder .
mexAtExit, mexMakeArrayPersistent, mexMakeMemoryPersistent, mxDestroyArray, mxFree, mxMalloc, mxRealloc
![]() | mxCalcSingleSubscript (C and Fortran) | mxChar (C) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |