Difference between dynamic and static allocation in Mex files
4 views (last 30 days)
Show older comments
what is the difference between allocating array in mex using mxMalloc and by declaring an array
example:
double a[200];
double* a = (double*)mxMalloc....
I want to use threads so I want to avoid using mxMalloc but I don't want to use malloc. can I use static allocation (I'll take bigger arrays with length parameter or something) thanks,
0 Comments
Answers (2)
Walter Roberson
on 14 Oct 2013
If you declare something as
double a[200];
inside a function, then it will be statically scoped, and will disappear when the function returns.
If you make the same declaration outside of any function, then it will be file scoped and will exist until the program exits.
If you use
extern double a[200];
outside of a function then you are making reference to a static global variable that will be accessible in any other file as well. At most one of the "extern" references at file scope can initialize the array.
Notice that static variables (declared at file scope) are fixed size and come into existence when the program is loaded. If you want something that does not exist until it is needed, or something that can be variable size, then you need to use dynamic allocation.
0 Comments
Jan
on 14 Oct 2013
If you use thread, it is not a question of wanting, but you must avoid mxMalloc from inside the threads. I do not see a reasons to avoid malloc and free.
A static allocation has the advantage and disadvantage, that the allocated memory is still occupied when the function returns. When you need 100MB for 16 threads, 1.6GB RAM are blocked until you clear the mex function from memory. This is much faster than allocating this memory dynamically each time the mex function is called.
4 Comments
James Tursa
on 14 Oct 2013
Edited: James Tursa
on 14 Oct 2013
@Eyal: One other difference I don't see mentioned is that declarations such as "double a[200]" typically cause the memory for the variable to be allocated off of the stack, which is relatively limited in size, as compared to malloc (and friends) which allocate memory off of the heap (*much* bigger than the stack).
Also, to get malloc'ed memory free'd if a mex routine exits abnormally one typically must employ the mexAtExit function (see the doc) and other cleaning functions (that you the programmer would write) and have the pointers themselves at the top level (outside any function) so that their values are not lost when the mex routine exits.
Jan
on 14 Oct 2013
@James: When a crashing Mex function is a rare exception, it might be enough to inform the user that a restart of Matlab is recommended. Am I right that this clears up leaked memory reliably?
For explanations about an exhausted stack space see http://en.wikipedia.org/wiki/Stack_overflow. Unfortunately these explanations are not clear: It depends on many parameters how large the stack is and if it is too small, the function crashes. Not really useful.
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!