What's the point of static arrays ?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jul 9 17:15:26 UTC 2020

On Thursday, July 9, 2020 10:21:41 AM MDT H. S. Teoh via Digitalmars-d-learn 
> > - Assignment copies the whole array, as in int[5] a; auto b = a;
> Sometimes this is desirable.  Consider the 3D game example.  Suppose
> you're given a vector and need to perform some computation on it. If it
> were a dynamic array, you'd need to allocate a new array on the heap in
> order to work on it without changing the original vector. With a static
> array, it's passed by value to your function, so you just do what you
> need to do with it, and when you're done, either discard it (== no work
> because it's allocated on the stack) or return it (== return on the
> stack, no allocations).

I recall that at one point, I wrote brute-force sudoku solver, and
initially, I'd used dynamic arrays to represent the board. When I switched
them to static arrays, it was _way_ faster - presumably, because all of
those heap allocations were gone. And of course, since the sudoku board is
always the same size, the ability to resize the array was unnecessary.

In most programs that I've written, it hasn't made sense to use static
arrays anywhere, but sometimes, they're exactly what you need.

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list