Understanding the source code of memcpy()

后端 未结 6 1946
野的像风
野的像风 2020-12-08 08:32
00018 void *memcpy(void *dst, const void *src, size_t len)
00019 {
00020         size_t i;
00021 
00022         /*
00023          * memcpy does not support overlappi         


        
6条回答
  •  自闭症患者
    2020-12-08 08:42

    I couldn't understand if part they do for integers. i < len/sizeof(long). Why is this calculation required ?

    Because they are copying words, not individual bytes, in this case (as the comment says, it is an optimization - it requires less iterations and the CPU can handle word aligned data more efficiently).

    len is the number of bytes to copy, and sizeof(long) is the size of a single word, so the number of elements to copy (means, loop iterations to execute) is len / sizeof(long).

提交回复
热议问题