How to pass static array to function not by value?

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 22 08:00:30 PST 2014


On 22.11.2014 20:26, Eric wrote:
> 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
>
>
>
>
Yes, it has limit in 16 mb (from here - 
http://dlang.org/arrays.html#resize):
"The total size of a static array cannot exceed 16Mb. A dynamic array 
should be used instead for such large arrays."

Thank you for answers.


More information about the Digitalmars-d-learn mailing list