Difference between dynamic and static allocation in Mex files
조회 수: 3 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Walter Roberson
2013년 10월 14일
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
Jan
2013년 10월 14일
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
James Tursa
2013년 10월 14일
편집: James Tursa
2013년 10월 14일
@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
2013년 10월 14일
@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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!