
From  "Zurab Sajaia" <zsajaia@hotmail.com> 
To  statalist@hsphsun2.harvard.edu 
Subject  Re: st: Mata: dropping structures from memory 
Date  Tue, 13 Jun 2006 13:51:12 0400 
Zurab Sajaia <zsajaia@hotmail.com> writes,
> I have a recursive problem involving linked list of structures in Mata, but
> after some number of chains (number is not fixed, that's why I decided to
> use list instead of a matrix) I don't need an earlier members of the list
> and they can be dropped to free some memory. If I don't drop them, my list
> is getting too long and even makes Stata to crash.
>
> I was wondering if there was some way of clearing structures like other
> objects in Stata.
I assume Zurab is using structure pointers. If not, I do not understand,
and Zurab will have to provide more information.
So let's imagine we have a structure like this:
struct node {
real scalar info
pointer(struct node) left
pointer(struct node) right
}
struct node is useful for creating binary trees.
So now imagine I'm inside a program, and variable x is a struct node.
Further assume that variable x is in the middle of the tree, so that
x.left != NULL and x.right != NULL.
Assume x.right points to a large branch of the tree. I now want to clear that
branch, and free all the nested structures. Just to be clear, *(x.right) is a
structure, and (x.right)>left might be another structure, and
(x.right)>right might be another, and if so, ((x.right)>left))>left might
be another structure, and ((x.right)>left))>right, and so on, and so on.
To chop of the tree at x.right, and free all memory associated with the
branch, I code
x.right = NULL
That is all there is no it!
Memory management is handled automatically by Mata. Mata will go through
the following logic:
I, Mata, have been asked to assign NULL to x.right. That is is
NULL is really not important, and I'll deal with that later.
First, however, I must deal with the fact that I have been asked
to assign something to x.right.
x.right might already contain something. I had better look.
Yes, I see it does. All right, let's drop and release the memory
associated with x.right.
Then Mata continues,
I, Mata, have been asked to release the memory of *(x.right).
*(x.right) is a structure, so to free it, I must free all the
elements of *(x.right). So I think I will tell myself to
free the first element of *(x.right), then free the second,
and so on.
Then Mata continues
I, mata, have been asked to free (*(x.right)).left.
I see that *(*(x.right).left) is a structure, so to free it, I must
free all the elements [...]
and Mata continues like that until there is no more  until it runs into
pointers containing NULL.
I mention all of that because, you might think that coding
x.right = NULL
doesn't do very much except assign NULL to x.right. In fact, Mata is
contantly keeping track of what is in use and what isn't, and freeing
anything it can.
 Bill
wgould@stata.com
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
* * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/
© Copyright 1996–2015 StataCorp LP  Terms of use  Privacy  Contact us  What's new  Site index 