How to pass in reference a fixed array in parameter

evilrat evilrat666 at gmail.com
Wed Jun 5 09:24:23 UTC 2024


On Wednesday, 5 June 2024 at 06:22:34 UTC, Eric P626 wrote:
> On Tuesday, 4 June 2024 at 16:19:39 UTC, Andy Valencia wrote:
>> On Tuesday, 4 June 2024 at 12:22:23 UTC, Eric P626 wrote:
>
> Thanks for the comments. So far, I only managed to make it work 
> by creating a dynamic array and keeping the same signature:
>
> ~~~
>     void main()
>     {  s_cell [][] maze = new s_cell[][](5,5);
>        print_maze (maze);
>     }
>
> void print_maze ( s_cell [][] maze )
> {
> }
> ~~~
>
> Now according to the book, it's possible to assign a slice from 
> a fixed array. This code will compile:
>
> ~~~
> int[12] monthDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 
> 30, 31 ];
> int[] a_slice = monthDays;
> ~~~

for simple cases like this it might work, but 2d array is not 
even contiguous, simpler case like s_cell[5][] might work too.

> How come the assignment does not work when passing a parameter. 
> I tried the following and it failed:
>
> ~~~
> s_cell [5][5] maze;
> s_cell [][] sliced_maze = maze;
> ~~~
>
> with this message:
>
> ~~~
> Error: cannot implicitly convert expression `maze` of type 
> `s_cell[5][5]` to `s_cell[][]`
> ~~~
>
> Is it because it's a 2D array (slice of slice)? I need to 
> manually copy each slice manually, or use a utility function to 
> do the copy? This is why it cannot auto-magically do it with 
> just when passing a parameter.
>

very likely this is the only solution - make a dynamic array by 
copying all elements.
there was few old bug tracker issues discussed wrt to static 
arrays and join function but there is seems to be no agreement so 
far.

>
> ~~~
> Error: function `mprmaze.create_maze(s_cell[][]* maze)` is not 
> callable using argument types `(s_cell[5][5]*)`
> cannot pass argument `& maze` of type `s_cell[5][5]*` to 
> parameter `s_cell[][]* maze`
> ~~~
>
> Now I think it expect a 2D array of pointers instead of a 
> pointer on a 2D array.
>
> It's also not clear if there is a difference between those 2 
> notations:
>
> ~~~
> &maze
> maze.ptr
> ~~~
>

there is, array itself is a tuple of length and pointer, the .ptr 
notation is just the data location, this is what you usually pass 
to C functions and not &maze itself.

to sum up, i wasn't able to make fixed-size arrays to work with 
dynamic arrays without making a copy, and I don't think this will 
change in the future because of various reasons including type 
system limitations and binary object formats.

so if you really absolutely need static arrays for example to 
avoid GC allocations in hot path than you need to make function 
that takes fixed size array.

in addition to that spec 
(https://dlang.org/spec/arrays.html#static-arrays) says static 
arrays is passed by value, unlike dynamic arrays that even when 
passed as length-and-pointer tuple will allow writing back to 
original data.




More information about the Digitalmars-d-learn mailing list