www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static array with parameter based size?

reply Miguel L <mlabayru gmail.com> writes:
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
next sibling parent Miguel L <mlabayru gmail.com> writes:
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 advance
Sorry, 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
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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
prev sibling parent reply Jack Applegame <japplegame gmail.com> writes:
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
parent Miguel L <mlabayru gmail.com> writes:
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:
 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);
Thank you very much for your answers.
Jul 12 2017