Boost logo

Boost :

Subject: Re: [boost] [type_traits] optimized std::copy example wrong?
From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2010-04-24 16:08:21


Steven Watanabe skrev:
> AMDG
>
> strasser_at_[hidden] wrote:
>> Zitat von Thorsten Ottosen <nesotto_at_[hidden]>:
>>
>>> 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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk