How to pass static array to function not by value?

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 22 07:35:11 PST 2014


On 22.11.2014 20:26, Ali Çehreli wrote:
> On 11/22/2014 07:07 AM, 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.
>
> If it overflows the stack just because there are two copies on it, then
> it will overflow for one of them in the future. (Say, after adding
> another variable to main.)
>
> For example, the following program has the same problem on my system
> without passing any argument:
>
> import std.stdio;
>
> enum A = 65536;
> enum B = 50;
>
> alias MyType = int[A][B];
>
> void main()
> {
> MyType arr;
> }
>
> make: *** [deneme] Segmentation fault
>
>  > I'd like to pass it not by value to avoid stack overflowing. Even if
> I use
>  > ref dmd pass it by value.
>
> I would dynamically allocate the storage. There is the following
> convenient syntax:
>
> import std.stdio;
>
> enum A = 65536;
> enum B = 1000; // <-- Much larger now
>
> alias MyType = int[][];
>
> void foo(ref MyType arr)
> {
> writeln(arr[3][40]);
> }
>
> void main()
> {
> auto arr = new int[][](B, A); // <-- "A elements of int[B] each"
> foo(arr);
> }
>
> Ali
>
I did like you suggested, thank you.


More information about the Digitalmars-d-learn mailing list