Is it safe to use memcpy
in the following scenario, where one is copying data from larger index into a block to smaller index in the same block. For example:
char buf[100];
// fill in the data ...
memcpy(&buf[10], &buf[15], 10);
In the above scenario I don't care about data from location 10 - 19 and am fine if its overwritten. Is there some reason why this should be avoided and memmove
used instead?
EDIT: Sorry I have not communicated my intention properly, so lets say I have data from index 10 - 19 and data from index 15 - 24, I want to copy data from 15 - 24 over 10 - 19, and I don't care about data from 10 - 19, is it safe to us memcpy
even though they are overlapping?
Edit:
Based on your edit, invert the following answer, since now you don't agree with the restrict
constraints.
Old answer
Yes it is safe. You are copying buf[10]
through buf[19]
on buf[20]
through buf[29]
. (Note that the first parameter of memcpy
is destination. So buf[10]
through buf[19]
are not being overwritten.)
memcpy
is defined in C11 as:
void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
notice the restrict
keyword. C11 at 6.7.3.8 says (emphasis mine):
An object that is accessed through a restrict-qualified pointer has a special association with that pointer. This association, defined in 6.7.3.1 below, requires that all accesses to that object use, directly or indirectly, the value of that particular pointer.135) The intended use of the restrict qualifier (like the register storage class) is to promote optimization, and deleting all instances of the qualifier from all preprocessing translation units composing a conforming program does not change its meaning (i.e., observable behavior).
In your example, buf[10]
through buf[19]
are accessed only through the s2
pointer and buf[20]
through buf[29]
are accessed only through the s1
pointer. Therefore, your usage of memcpy
is perfectly ok.
In simpler terms, as long as the arrays you give to memcpy
don't overlap, it's ok.