How to pass static array to function not by value?

Eric via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 22 08:26:58 PST 2014


On Saturday, 22 November 2014 at 16:07:25 UTC, drug wrote:
> On 22.11.2014 19:34, ketmar via Digitalmars-d-learn wrote:
>> On Sat, 22 Nov 2014 18:20:44 +0400
>> drug via 
>> Digitalmars-d-learn<digitalmars-d-learn at puremagic.com>  wrote:
>>
>>> I tried to pass pointer to static array but it didn't work.
>> i tried it right now and it works.
>>
>> if you really want to get some help, you'd better give us 
>> something to
>> start with. i.e. your code, minified. D is great, but it still 
>> can't
>> grant telepathic abilities to us.
>
> Sorry for inconvenience.
> http://dpaste.dzfl.pl/64ab69ae80d2
> this causes stackoverflow because static array is big enough. 
> I'd like to pass it not by value to avoid stack overflowing. 
> Even if I use ref dmd pass it by value.

Your problem is not the reference issue.  D has a limit on how big
static arrays can be in a function.  You can make them bigger by 
declaring them
globally, but I think even that has a limit.  Try this:

import std.stdio;

enum A = 65536;
enum B = 32;

alias MyType = int[A][B];

void foo(ref MyType arr)
{
     writeln(arr[3][3]);
}

MyType arr;
void main()
{
     writeln("arr[3][3] = ", arr[3][3]);
     foo(arr);
}

-Eric






More information about the Digitalmars-d-learn mailing list