A buffer's contents must be shifted three bytes to the right inside the same array. Why is memmove required instead of memcpy?
Correct answer
A. memmove handles overlap by choosing a safe copy direction, while memcpy does not
16 views
A. memmove handles overlap by choosing a safe copy direction, while memcpy does not
16 views
memmove behaves as if the source were copied into a temporary buffer first, so overlapping source and destination produce the shifted result correctly. memcpy requires non-overlapping regions; passing overlapping ranges is undefined behaviour that may silently corrupt data. Note that identical pointers count as overlap, so only memmove covers that case portably.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.