External Interfaces/API Reference    
mxCalloc

Allocate dynamic memory using the MATLAB memory manager

C Syntax

Arguments

n
   Number of elements to allocate. This must be a nonnegative number.

size
   Number of bytes per element. (The C sizeof operator calculates the number of bytes per element.)

Returns

A pointer to the start of the allocated dynamic memory, if successful. If unsuccessful in a stand-alone (nonMEX-file) application, mxCalloc returns NULL. 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.

Description

MATLAB applications should always call mxCalloc rather than calloc to allocate memory. Note that mxCalloc works differently in MEX-files than in stand-alone MATLAB applications.

In MEX-files, mxCalloc automatically

The MATLAB memory management facility maintains a list of all memory allocated by mxCalloc. The MATLAB memory management facility automatically frees (deallocates) all of a MEX-file's parcels when control returns to the MATLAB prompt.

In stand-alone MATLAB applications, mxCalloc defaults to calling the ANSI C calloc function. If this default behavior is unacceptable, you can write your own memory allocation routine, and then register this routine with mxSetAllocFcns. Then, whenever mxCalloc is called, mxCalloc calls your memory allocation routine instead of calloc.

By default, in a MEX-file, mxCalloc generates nonpersistent mxCalloc data. In other words, the memory management facility automatically deallocates the memory as soon as the MEX-file ends. If you want the memory to persist after the MEX-file completes, call mexMakeMemoryPersistent after calling mxCalloc. 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.

When you finish using the memory allocated by mxCalloc, call mxFree. mxFree deallocates the memory.

Examples

See explore.c in the mex subdirectory of the examples directory, and phonebook.c and revord.c in the refbook subdirectory of the examples directory.

For additional examples, see mxcalcsinglesubscript.c, mxsetallocfcns.c, and mxsetdimensions.c in the mx subdirectory of the examples directory.

See Also

mxFree, mxDestroyArray, mexMakeArrayPersistent,
mexMakeMemoryPersistent, mxMalloc, mxSetAllocFcns


  mxCalcSingleSubscript mxChar