Understanding the source code of memcpy()

后端 未结 6 1948
野的像风
野的像风 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:55

    len%sizeof(long) checks if you are trying to copy full-longs not a part of long.

    00035    if ((uintptr_t)dst % sizeof(long) == 0 &&
    00036             (uintptr_t)src % sizeof(long) == 0 &&
    00037             len % sizeof(long) == 0) {
    00038 
    00039                 long *d = dst;
    00040                 const long *s = src;
    00041 
    00042                 for (i=0; i

    checks for alignment and if true, copies fast(sizeof(long) bytes at a time).

    00046    else {
    00047                 char *d = dst;
    00048                 const char *s = src;
    00049 
    00050                 for (i=0; i

    this is for the mis-aligned arrays (slow copy (1 byte at a time))

提交回复
热议问题