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.