I'm writing to get some general advice from more advanced Julia users than me 
(which is probably most users). Coming from Matlab, the concepts of 
pass-by-reference and array-views initially scared me a bit (in terms of 
generating hard to find bugs). Pass-by-reference is a bit less scary when using 
the syntactic convention foo!(x,y) but I'm still a bit wary of the array-view 
concept. This came up when I recently realized that the output of vec(x) and 
reshape(A, dims) can share memory with it's arguments. Wow, I had no idea. 
Indeed, the only way I would know that vec shared memory is if I happened to 
test it:

a = rand(2,2)
b = vec(a)
b[1] = 0.0
a[1] == 0.0 #<- true


The documentation doesn't give a hint:

help> vec
INFO: Loading help data...
Base.vec(Array) -> Vector

   Vectorize an array using column-major convention.


Is there a general way I can extrapolate when a function's return value will 
share memory with inputs? For example, I could imagine diag, diagm, transpose, 
reshape, hcat, vcat could all possibly sharing memory with inputs but I would, 
a-priori have no idea which? To me, this situation warrants another syntactic 
convention like foo%, foo> or foo*. 

Besides wanting to know this for myself, I'm considering using Julia in my 
intro stats classes. Having to explain shared memory stuff is too complicated 
for beginners, in my opinion, and having a syntactic convention would allow me 
to say..."stay away from those unless you know what your going".

Thanks! 

Reply via email to