[type_traits] optimized std::copy example wrong?

Hi John & co, IIRC, std::copy() cannot use memcpy(), since the memory is allowed to overlap. Therefore it can only use memmove(). regards -Thorsten

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 In Christ, Steven Watanabe

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

On 24 Apr 2010, at 16:55, 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()
std::copy() does allow overlapping ranges. It requires the start of the range being copied to is not in the range from, but the end can be. std::copy(it, it + 5, it - 1); is legal. The equivalent memcpy(it, it - 1, 5); is not For exactly this reason, libstdc++ uses memmove rather than memcpy. memmove can also be used for copy_backward. Chris

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()
std::copy() does allow overlapping ranges. It requires the start of the range being copied to is not in the range from, but the end can be.
std::copy(it, it + 5, it - 1); is legal. The equivalent memcpy(it, it - 1, 5); is not
For exactly this reason, libstdc++ uses memmove rather than memcpy. memmove can also be used for copy_backward.
Updated examples and docs in Trunk, Thanks for catching this, John.
participants (5)
-
Christopher Jefferson
-
John Maddock
-
Steven Watanabe
-
strasser@uni-bremen.de
-
Thorsten Ottosen