next up previous contents index
Next: The memcpy function Up: INTRODUCTION Previous: String function conventions

Copying functions

A block copy routine should be ``right'': it should work correctly even if the blocks being copied overlap. Otherwise it is more difficult to correctly code such overlapping copy operations, and portability suffers because the optimal C-coded algorithm on one machine may be horribly slow on another.

A block copy routine should be ``fast'': it should be implementable as a few inline instructions which take maximum advantage of any block copy provisions of the hardware. Checking for overlapping copies produces too much code for convenient inlining in many implementations. The programmer knows in a great many cases that the two blocks cannot possibly overlap, so the space and time overhead are for naught.

These arguments are contradictory but each is compelling. Therefore the Standard mandates two block copy functions: memmove is required to work correctly even if the source and destination overlap, while memcpy can presume nonoverlapping operands and be optimized accordingly.

4 11 2 1




Pete Jinks
Fri Jan 19 12:31:56 GMT 1996