I am trying to write a generic multidimensional algorithm where I need a random access to an ND Array.
I need to define a CartesianIndex with computed coordinates and modify my array using this CI.

Unfortunately it seems that this process allocates some memory:

function my_algo(ndarray::Array{Int,D}) where {D}
temp_idx_array=zero(Int,D)
for ...
myupdate!(temp_idx_array)
ndarray(CartesianIndex(Tuple(temp_idx_array))=something
end

I could compute the corresponding linear index to ndarray but the rest of the algorithmis based on CartesianIndices.

Thanks !
The length of the vector is known when the algorithm is applied to the ND array so I could use a parametric algorithm like in my pseudo_example. The problem is then to create the input Tuple without relying on multi-argument Ctor.

The linear index solution is probably much simpler in my case…

Since CartesianIndex are just fancy tuples & immutable, I usually create whole grids of them and filter the ones out I don’t care about.

The length of the vector is not known on the type level, which causes a type instability for the Tuple, which in turn causes a type instability on CartesianIndex (the dimensionality is part of the type there after all). These type instabilities end up boxing those variables, meaning they need to be heap allocated - 2 type instabilities plus one array equals 3 allocations

Thank you very much for all answers not only providing a neat solution but allowing me to understand what was going on

explains nicely why I marked @rafael.guerraStaticArrays based proposal as the solution of my problem:

In my code, I can use the same mutable and fixed size MVectorx which is modified inside the loop and compute corresponding CartesianIndex(Tuple(x)) for free to access my ND-Array.

Sorry for the pseudo-algo I provided which was not a MWE.