Please enable JavaScript to view this site.

Vensim Help

Vensim provides a simple memory allocation function that is appropriate for allocating memory that will remain valid while a simulation is in progress, and then automatically be freed. This can either be called using the callback address provided in VENGV (the preferred method),

void *(VEFCC *alloc_simmem)(DATA_MATRIX *dm,CONSTANT_MATRIX *cm,V4BYTEU bytes) ;

or by linking with vensim.lib and calling the function vensim_alloc_simmem.

void* VEFCC vensim_alloc_simmem(DATA_MATRIX *dm,CONSTANT_MATRIX *cm,V4BYTEU bytes) ;

dm  a pointer to a DATA_MATRIX structure.  Before calling alloc_simmem you need to fill in the number of times in the DATA_MATRIX (the number of variables will have been filled in by Vensim).  The memory that is allocated will be accessed throughout the simulation so it is not recommended that you use an alternate memory allocation mechanism for data matrices.

cm a pointer to a CONSTANT_MATRIX structure.  Both the number of rows and columns required in a CONSTANT_MATRIX are specified by Vensim before it passes the structure to an external function.  The use of the callback to vensim_alloc_simmem is designed to allow you to use an alternate memory allocation mechanism.  The memory required for a constant will only be referenced once, and before any other external functions are called.  Because of this you can use the same memory for different calls to constant definition functions without problems.

bytes is the number of bytes of memory to allocate.  The return value is a pointer to those bytes.

Return Value:  alloc_simmem returns a pointer to the amount of memory specified by bytes (and initialized to zero).  If memory allocation fails Vensim will throw a fatal error and exit.  Thus you do not need to check the return value.  If bytes is 0 then the function returns NULL.

Note that alloc_simmem would normally only be called with one non null argument.  You can, however, pass two or all three and it will do the allocation for each non null argument it receives.