使用D切片的memset()和memcpy()

时间:2015-08-15 17:58:12

标签: d memcpy memset compiled-language ctfe

在D语言中,以下语句的等价物是什么 假设代码: -

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))

我在想转换是: -

skip[0 .. size-1] = skip[shift .. size-1   ];  //For the memcpy();
skip[0 .. size-1] = 0;                         //For the  memset();

但这似乎对我不起作用,因为dmd(v2.066.1)给出错误slice [8..7] exceeds array bounds [0..8]

1 个答案:

答案 0 :(得分:1)

我认为m表示memcpy / memset代码中数组的长度。

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

请注意,如果数组边界重叠,您将在第一行获得运行时错误。

相关问题