Hi,
i was wondering if there is any detailed documentation on how Mata manages memory, e.g. if I should think in terms of stack and heap-allocated memory.
I particular, I realized that in my functions I often return pointers to locally declared/allocated matrices, which would be a mistake if this was C/C++ as the stack-based objects would be destroyed when the function terminates. In Mata, however, it seems to work.
This, however, prompts the question when the memory of objects that have been allocated within a function is actually freed. This post [http://www.stata.com/statalist/archi.../msg00494.html ] suggests that setting a pointer to NULL actually triggers memory to be freed. For example, would this be enough to deallocate matrix m?
But then I wonder if Mata implements some sort of reference counting? After all, I could store more pointers to the same memory region in different places. So is a Mata pointer a "naked" C pointer or more like a C++ shared_ptr?
Thanks for any comments!
Richard
i was wondering if there is any detailed documentation on how Mata manages memory, e.g. if I should think in terms of stack and heap-allocated memory.
I particular, I realized that in my functions I often return pointers to locally declared/allocated matrices, which would be a mistake if this was C/C++ as the stack-based objects would be destroyed when the function terminates. In Mata, however, it seems to work.
This, however, prompts the question when the memory of objects that have been allocated within a function is actually freed. This post [http://www.stata.com/statalist/archi.../msg00494.html ] suggests that setting a pointer to NULL actually triggers memory to be freed. For example, would this be enough to deallocate matrix m?
Code:
mata: mata clear pointer(real matrix) foo() { real matrix m m = J(10,10,0) return(&m) } m2 = foo() m2 = NULL end
Thanks for any comments!
Richard
Comment