memmov conflicts

May 15, 2008 at 11:45am
Well people, I got this doubt when reading about the memmov function in href=http://www.cplusplus.com/reference/clibrary/cstring/memmove.html
. The prototype is given as
void * memmove ( void * destination, const void * source, size_t num ); /*source is a pointer to a constant location. Hence the data pointed to by 'source' cannot be changed. */
Now consider the following statement
"Copying takes place as if an intermediate buffer was used, allowing the destination and source to overlap."
Dont you think the prototype given and this statement are mutually conflicting since if the source and dest overlap and it is guaranteed that the dest will finally contain the data pointed to by dest, source will finally contain data which is not the same which it had previously. What do you guys say?

Even in case memcpy, source is const void*. Hence in case of overlap it can choose not to change the source content since the behavior is undefined(What else can it do?) for memcpy.
May 15, 2008 at 4:10pm
Dont you think the prototype given and this statement are mutually conflicting since if the source and dest overlap and it is guaranteed that the dest will finally contain the data pointed to by dest, source will finally contain data which is not the same which it had previously.


No. The prototype only guarantees that the data pointed to by source will not be modified via the source pointer. It doesn't guarantee what may or may not be modified via the destination pointer, which is non-const. The purpose of the function is clearly defined and it is stated that an intermediate buffer is used specifically to allow an overlapping pair of pointers and that it is up to the user to use the function wisely. You could write your own function that takes a const and a non-const pointer and use the non-const pointer to write to the same data pointed to by the const pointer. This is not something that people normally do, but they could if they wanted to. A pointer variable is only one doorway to the data. Declaring one particular variable to be const does not mean that the data pointed to by *it* cannot be modified in some other way.
May 16, 2008 at 4:43am
Thanks for clearing up the misconception.
Last edited on May 16, 2008 at 4:48am
Topic archived. No new replies allowed.