Memory Overlap and memcpy function.
Hello everybody. :)
Code:
#include<stdio.h>
#include<string.h>
int main(void)
{
char str[20] = "HELLOSIR";
memcpy( str + 2 , str + 1 , 4 );
puts( str );
return 0;
}
If I have understood well the above code can be a typical example that decribes a memory overlap. Some of data to the destination (str + 2 ) will be copied before its copy.
Code:
void *memcpy( void * restrict s1, const void * restrict s2, size_t n );
According to the above example I think there is no quarantee even the restrict to the pointer that we won't have overlap.It is legal to use the same pointer and not other in order to have access on the data.So for this the behaviour is not undefined right?
But how memcpy works? I mean I am taking
as output rather than
So the behaviour due to overlap is undefined?
p.s The const on const void * restrict s2 denotes that data can't change from s2 itself?