How to pass static array to function not by value?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 22 08:47:14 PST 2014


On Sat, 22 Nov 2014 19:07:14 +0400
drug via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> 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.
ah, that's why we need to look at the code! ;-)

what you have here is multidimensional static array, so 'ref' is not
working as you expected. let's just say that you can't do this with
'ref' and static arrays, 'cause compiler will copy some of the data
anyway.

multidimensional static array is just a continuous region of memory, not
a pointer to regions, as dynamic arrays. so compiler can't make
reference to `[A][B]` (for now, at least). either use dynamic arrays as
Ali suggested, or do all the pointer math manually (i.e. pass &arr[0]0]
and all dimensions).

it's a bad practice to have such big arrays on the stack anyway.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141122/7a360f9b/attachment.sig>


More information about the Digitalmars-d-learn mailing list