In Julia, all arguments to functions are passed by sharing (i.e. by pointers). Some technical computing languages pass arrays by value, and while this prevents accidental modification by callees of a value in the caller, it makes avoiding unwanted copying of arrays difficult. By convention, a function name ending with a ! indicates that it will mutate or destroy the value of one or more of its arguments (compare, for example, sort and sort!). Callees must make explicit copies to ensure that they don’t modify inputs that they don’t intend to change. Many non- mutating functions are implemented by calling a function of the same name with an added ! at the end on an explicit copy of the input, and returning that copy.
julia> function Append!(vectorOfVector, vector)
vector[1] = 0
push!(vectorOfVector,deepcopy(vector))
vector[1] = 1
push!(vectorOfVector,deepcopy(vector))
end
Append! (generic function with 1 method)
julia> vectorOfVectors = Vector{Vector{Int64}}()
0-element Array{Array{Int64,1},1}
julia> vector = Vector{Int64}(undef,1)
1-element Array{Int64,1}:
0
julia> Append!(vectorOfVectors, vector)
2-element Array{Array{Int64,1},1}:
[0]
[1]
Hi oheil,
Thanks for your replying. I tested your code and it works well.
But I don’t really understand why making a copy of vector can make change. If use vector directly (not copy(vector)), it should be [0] and [1] before being pushed in the two times respectively. So the two push! functions equal push!(vectorOfVector, [0]) and push!(vectorOfVector, [1]). But how the second-time push! function changed the first value of vectorOfVector?
we only have a single instance/memory allocation of vector. Every change on a value at some index of vector will be visible in vector itself, at index 1 of vectorOfVector and at index 2 of vectorOfVector, because its all pointing to the same memory.
So the two push! functions equal push!(vectorOfVector, [0]) and push!(vectorOfVector, [1]).
And thats not true. [0] and [1] above are two different vectors/arrays pointing to different locations in memory.
You can think of it as always being true. Doing x= something wouldn’t change the value in the array even in the case where the value is an array. In fact, an assignment can never have an observable effect in some other object.
It will never be wrong to think of all objects being passed via pointer (modulo C interop). Some objects just happen to be immutable.