Passing a ubyte[] to a function taking a 'ref ubyte[16]'

simendsjo simendsjo at gmail.com
Sat Jun 23 11:33:09 PDT 2012


On Sat, 23 Jun 2012 20:23:26 +0200, Johannes Pfau <nospam at example.com>  
wrote:

> So how to pass a ubyte[] to a function expecting a ref ubyte[16]
> without allocating/using any extra memory (Not even stack)?
> This seems to work, but it's very ugly:
> ------------
> _digest.finish(*cast(ubyte[16]*)buf.ptr);
> ------------
> I thought this might create a temporary, but it passes all unittests,
> so it seems to work?

I don't have a clue, but the following seems to work as well :)

void sarr(ref ubyte[1] a) {
     darr(a.ptr[0..a.length]);
}

void darr(ubyte[] a) {
     a[0] = 2;
}

void main() {
     ubyte[1] a = [1];
     sarr(a);
     assert(a[0] == 2);
}


More information about the Digitalmars-d-learn mailing list