Pointers or copies?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Dec 21 07:26:56 PST 2006


Chris Nicholson-Sauls wrote:
> void drop (T) (inout T[] haystack, size_t index)
> in {
>   assert(index < haystack.length, ".drop() called with index greater 
> than array length");
> }
> body {
>   if (index != haystack.length - 1) {
>     memmove(&(haystack[index]), &(haystack[index + 1]), T.sizeof * 
> (haystack.length - index));
>   }
>   haystack.length = haystack.length - 1;
> }

Doesn't that move one element from beyond the end of the array?

Array [0, 1, 2], drop element at index 1:
	memmove(&(array[1]), &(array[2]), int.sizeof * (3-1));
Moves 2 elements, starting at the 3rd element (element at index 2) of a 
3-element array...



More information about the Digitalmars-d mailing list