Friday, July 29, 2011

Things to Remember about memcpy()

If you want to implement memcpy(), you need to pay attention to the following things:
  1. if you can use wider data type, use it (e.g., copy a 32 bit instead of 8 bit).
  2. leverage the instruction set provided by the underlying architecture (processor). For example, some architecture has *p++, some only has *(++p).
  3. memory alignment. Sometimes if you want to do 32bit copy, the instruction may require memory address to be aligned. Therefore some extra work needs to be done.
  4. use pointer such as const char * for src.
  5. if the src and dest memory address overlap with each other, some extra work needs to be done.

No comments:

Post a Comment