| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
July 12, 2017 Static array with parameter based size? | ||||
|---|---|---|---|---|
| ||||
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
| ||||
July 12, 2017 Re: Static array with parameter based size? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Miguel L | 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.
| |||
July 12, 2017 Re: Static array with parameter based size? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Miguel L | 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. | |||
July 12, 2017 Re: Static array with parameter based size? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Miguel L | 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);
| |||
July 13, 2017 Re: Static array with parameter based size? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | 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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply