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:30:58 PST 2014


On Sat, 22 Nov 2014 08:07:31 -0800
"H. S. Teoh via Digitalmars-d-learn"
<digitalmars-d-learn at puremagic.com> wrote:

> On Sat, Nov 22, 2014 at 05:57:30PM +0200, ketmar via Digitalmars-d-learn wrote:
> > On Sat, 22 Nov 2014 15:45:51 +0000
> > Eric via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:
> > 
> > > Maybe this is not so lame because change() can take
> > > any length of static array.
> > 
> >   void change (int[] arr) {
> >     arr[1] = 42;
> >   }
> > 
> >   void main () {
> >     int[$] a = [1, 2, 3];
> >     change(a);
> 
> This is actually really scary that static arrays implicitly decay to a
> dynamic array slice. It can cause all sorts of problems with escaping
> dangling references to local variables:
i don't even want to argue with that. ;-) just 'cause i'm sure that you
are right, and this code shouldn't compile at all without explicit
slicing of `a`.

but this is what we have now, and i bet it will stay with us, 'cause
"changing that will break alot of correct D code". so if we can't
annihilate it, let's use it! ;-)
-------------- 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/67186cc3/attachment.sig>


More information about the Digitalmars-d-learn mailing list