1. Home
  2. Computer Science MCQs
  3. C Programming
  4. A buffer's contents must be shifted three bytes to the…

Added 30 Nov -0001

A buffer's contents must be shifted three bytes to the right inside the same array. Why is memmove required instead of memcpy?

  1. memmove handles overlap by choosing a safe copy direction, while memcpy does not Correct answer
  2. memcpy refuses overlapping ranges at run time and returns a null pointer instead
  3. memmove is needed only when the two ranges are exactly equal, not when they overlap
  4. memcpy copies through a hidden temporary buffer, so it needs twice the stack space

Correct answer

A. memmove handles overlap by choosing a safe copy direction, while memcpy does not

16 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs