Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Strange behaviour with Mata compiler


From   "Mak, Timothy" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: Strange behaviour with Mata compiler
Date   Wed, 22 Dec 2010 17:44:46 +0000

Dear list, 

While I was debugging a program, I discovered some strange behaviour with the Mata compiler. Can somebody show me why? 

If we run the following code,  

// Begin code
struct test {
	real scalar a
}

function jkl() {
	struct test scalar j, k
	j.a = 1
	k.a = 2
	j
	k

	liststruct(j)
	liststruct(k)

	k = j

	j
	k

}

jkl()

// End code

In the above code, j and k are given separate pointers. Even when I assigned k = j, the pointers for j and k remain separate. 
However, if I have an additional layer of structure, e.g. 

// Begin code
struct test2 {
	struct test scalar j
	struct test vector k
}

function jkl2() {

	struct test2 scalar S
	S.j.a = 4
	S.k = J(4, 1, test())
	S.k[1].a = 6

	S.j
	S.k[1]

	liststruct(S.j)
	liststruct(S.k[1])

	S.j = S.k[1]

	S.j
	S.k[1]

}
 
jkl2()
// End code

Then, I found that when I assigned S.j = S.k[1], the pointer for S.j takes on the value of S.k[1], unlike in the above. This is quite unexpected. Because if I then try to modify S.j, say by doing S.j.a = 20, the value of S.k[1].a changes also (to 20), and vice versa if I modify S.k[1]. 

However, on the other hand if I run the following: 

// Begin code
function jkl3() {

	struct test2 scalar S
	S.j.a = 4
	S.k = J(4, 1, test())
	S.k[1].a = 6
	
	S.j
	S.k[1]

	liststruct(S.j)
	liststruct(S.k[1])

	S.k[1] = S.j

	S.j
	S.k[1]

}
 

jkl3()
// End code

S.k[1] and S.j retain their separate pointer address. 

Can someone tell me what the rationale is behind this behaviour? 

Thanks a lot, 

Tim

BTW I'm running Stata 10.1 on Windows. 



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index