using memset withing a pure function

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 15 11:49:14 PDT 2015


On Saturday, 15 August 2015 at 18:04:30 UTC, D_Learner wrote:
>     memcpy(&skip[0], &skip[0]+shift, (m-shift)*(int.sizeof));
>     memset(&skip[0]+(m-shift),0, shift*(int.sizeof))
>
> I was thinking conversion would be :-
>
>     skip[0 .. size-1] = skip[shift .. size-1   ];  //For the 
> memcpy();

Those two slices have different lengths (when shift != 0). They 
must have equal lengths, and they must not overlap.

>     skip[0 .. size-1] = 0;    //For memset()
>
> But this doesn't seem to work for me as dmd(v2.066.1) gives the 
> "error slice [8..7] exceeds array bounds [0..8]"  .Sure am 
> missing something.

You can just do mechanical translations.
   memcpy(dst, src, n * T.sizeof);
becomes
   dst[0 .. n] = src[0 .. n];
Then simplify.

Original:
   memcpy(&skip[0], &skip[0]+shift, (m-shift)*(int.sizeof));
Identify the pieces:
   dst = &skip[0]
   src = &skip[0]+shift
   n = m-shift
Substitute:
   (&skip[0])[0 .. m-shift] = (&skip[0]+shift)[0 .. m-shift];
Simplify:
   skip[0 .. m-shift] = skip[shift .. $][0 .. m-shift];
   skip[0 .. m-shift] = skip[shift .. shift + m-shift];
   skip[0 .. m-shift] = skip[shift .. m];

I sure hope I didn't mess anyting up.

memset is very similar.
   memset(dst, val, n * T.sizeof);
becomes
   dst[0 .. n] = val;
Then simplify.



More information about the Digitalmars-d-learn mailing list