Could someone help me put logic into the behavior of pointers, when they point on views of temporary variables?
Here is a toy example:
p is a Mata view on a temporary variable, which contains the content of the variable weight. pp is a pointer on this view. The above code computes the mean of whatever pp points to 4 times. In the first 3 instances, it correctly returns the mean of the variable weight. I have no idea what it returns in the last instance.
I am aware of the issue that Mata views are based on variable indices, not variable names. When temporary variables are deleted, the view supposedly switches to a different variable. What I do not understand is the following:
Any insights would be appreciated.
Here is a toy example:
Code:
clear all sysuse auto program define testprog tempvar touse nonsense weight gen byte `touse' = 1 gen byte `nonsense' = 0 gen `weight' = weight mata: st_view(p = ., ., "`weight'", "`touse'") mata: pp = &p mata: mean(*pp) if `0' { drop `nonsense' } end testprog 0 mata: mean(*pp) testprog 1 mata: mean(*pp)
I am aware of the issue that Mata views are based on variable indices, not variable names. When temporary variables are deleted, the view supposedly switches to a different variable. What I do not understand is the following:
- Why does the second output still equal the mean of weight, even though it is computed after the temporary variables created in the program testprog are deleted when the program terminates?
- Why does it make a difference whether I explicitly delete a temporary variable at the end of a program or not?
- What is the pointer pointing to when called the last time? There is no variable in the data set with the mean that is returned here.
Any insights would be appreciated.
Comment