memset() and memcpy() using D slices

265 views Asked by At

In the D language, what are the equivalents to the following statements assuming the code :-

int size = 8;
int shift = 1; 
int[size] skip;
int[size]  suff;

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();
skip[0 .. size-1] = 0;                         //For the  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] .

1

There are 1 answers

0
sigod On

I assume m represents length of the array in your memcpy/memset code.

skip[0 .. size - shift] = skip[shift .. size]; // may throw
skip[size - shift .. size] = 0;

Note that you'll get runtime error on first line if array bounds overlap.