
Steven Watanabe skrev:
AMDG
strasser@uni-bremen.de wrote:
Zitat von Thorsten Ottosen <nesotto@cs.aau.dk>:
Hi John & co,
IIRC, std::copy() cannot use memcpy(), since the memory is allowed to overlap. Therefore it can only use memmove().
std::copy() doesn't allow overlapping ranges. -> std::copy_backward()
Both copy and copy_backwards allow some overlapping ranges.
int array[10] = {}; std::copy(&array[0], &array[7], &array[1]); // illegal std::copy(&array[1], &array[8], &array[0]); // okay std::copy_backwards(&array[0], &array[7], &array[8]); // okay std::copy_backwards(&array[1], &array[8], &array[7]); // illegal
Ok, so what is the conclusion? From the C standard: void *memcpy(void * restrict s1, const void * restrict s2, size_t n); Description [#2] The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined. I recall that STLPorts std::copy() used memmove(). How do we interpret "objects that overlap"? -Thorsten