digitalmars.D.learn - Static array with parameter based size?
- Miguel L (18/18) Jul 11 2017 Hi
- Miguel L (4/22) Jul 11 2017 Sorry, this post is duplicated. Yesterday forum was not working
- Adam D. Ruppe (25/33) Jul 12 2017 Then just don't change the length... this is a correct way to do
- Jack Applegame (6/8) Jul 12 2017 void foo(size_t N)(ref int[N] arr) {
- Miguel L (2/10) Jul 12 2017 Thank you very much for your answers.
Hi I need to create a non-dynamic array like this void f(int x) { int[x] my_array; ... this does not compile as x value needs to be known at compile time. The closest to this I can get is: void f(int x) { int[] my_array; my_array.length=x; but I don't really need a dynamic array as length is not going to change inside f. What is the best way to do this? Also what is it possible in D to write a function that accepts an static array of any size? Thanks in advance
Jul 11 2017
On Wednesday, 12 July 2017 at 05:45:13 UTC, Miguel L wrote:Hi I need to create a non-dynamic array like this void f(int x) { int[x] my_array; ... this does not compile as x value needs to be known at compile time. The closest to this I can get is: void f(int x) { int[] my_array; my_array.length=x; but I don't really need a dynamic array as length is not going to change inside f. What is the best way to do this? Also what is it possible in D to write a function that accepts an static array of any size? Thanks in advanceSorry, this post is duplicated. Yesterday forum was not working and i received an error when i was trying to post it. Please ignore it.
Jul 11 2017
On Wednesday, 12 July 2017 at 05:45:13 UTC, Miguel L wrote:void f(int x) { int[] my_array; my_array.length=x; but I don't really need a dynamic array as length is not going to change inside f.Then just don't change the length... this is a correct way to do it. You could also allocate it with `alloca` or `malloc` depending on exact use. I also sometimes like to just slice a static array: ``` void f(int x) { int[1000] buffer; int[] my_array = x < buffer.length ? buffer[0 .. x] : new int[](x); } ``` Which gives the speed benefits of static without putting a size limit on it. Just make sure you keep track of ownership with any of these strategies.Also what is it possible in D to write a function that accepts an static array of any size?Best option is to just accept a slice: void f(int[] x); and call it like so: int[123] some_static_array; f(some_static_array[]); That will work on any size and typically give best performance. Again though, just use caution about ownership when using static arrays.
Jul 12 2017
On Wednesday, 12 July 2017 at 05:45:13 UTC, Miguel L wrote:Also what is it possible in D to write a function that accepts an static array of any size?void foo(size_t N)(ref int[N] arr) { ... } int[10] arr; foo(arr);
Jul 12 2017
On Wednesday, 12 July 2017 at 18:49:23 UTC, Jack Applegame wrote:On Wednesday, 12 July 2017 at 05:45:13 UTC, Miguel L wrote:Thank you very much for your answers.Also what is it possible in D to write a function that accepts an static array of any size?void foo(size_t N)(ref int[N] arr) { ... } int[10] arr; foo(arr);
Jul 12 2017